Question

In: Computer Science

Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...

Question 31

Given the code snippet below, what prints?

void fun(int *p)

{

int q = 10;

p = &q;

}

int main()

{

int r = 20;

int *p = &r;

fun(p);

cout << *p;

return 0;

}

Question 31 options:

10

20

compiler error

Runtime error

Question 32

A union’s members are exactly like the members of a Structure.

Question 32 options:

True
False

Question 33

Given the code below, what are the errors.

#include <iostream>

using namespace std;

int main()

{

int x, *p;

x=15;

ind_square(*p);
}

ind_square(int * p)

{

*p = *=p **p;

}

Question 33 options:

Undeclared identifier or function ind_square

“*=” this expects an expression

No type specifier for ind_square

All of the above

Question 34  

Given the structure definition below, a member of the structure can be accessed by one of the following statements:


struct Employee{
string emName;
string emSex;
string emAddress;
int emSalary;
};
Employee Emp;

Question 34 options:

Employee.emName;

Emp.emName;

Emp->emName;

Question 35

Using the structure below, which of the following statements about creating an array (size 20) of structures are not true?
struct Employee{
    string emp_id;
    string emp_name;
    string emp_sex;
};

Question 35 options:

Employee emp[20];

Empoyee[] emp=new Employee[20];

Employee[20] emp;

Question 36

What are the uses for the * operator.

Question 36 options:

Multiplication

Pointer declaration

Deallocation

Question 37

Question 37 options:What would be printed from the following C++ program?

#include <iostream>
#include <string>
using namespace std;

void printinfo(int);
void printinfo(string);

struct student //define structure student
{//variables

int id;
string name;

};
int main()
{


student st; //structure object
st.id = 100; //assign 100 to student id
st.name = "Sok";//assign Sok to student name
//output student information
printinfo(st.id);
printinfo(st.name);;
return 0;


}

void printinfo(int id){
cout<<id<<"\n";
}


void printinfo(string name)
{
cout<<name<<"\n";
}

Question 38

Which of the following statements about the structure is not true?

Question 38 options:

To use a structure, previously it is defined.

A structure can have different types of variables in it.

A structure can't have arrays in it.

All of the above

Question 39

The _____i s the name of the structure, and the _____ declared inside the braces of the structure are called the ______ of the structures.

Question 40

Use the correct function to print the length of the txt string.

string txt = “Hello”;

cout <<   

;

Solutions

Expert Solution

Question 31

Answer: 20

Pointers are also passed by value. So when p is passed to fun what ever changes made in fun is not affected in main function, it will still points to the address &r.

Question 32

False

Union members share share a single memory location, while structure members have individually allocated spaces.

Question 33

All of the above

Question 34  

Emp.emName;

Question 35

Empoyee[] emp=new Employee[20];

Employee[20] emp;

Are false

Question 36

Multiplication

Pointer Declaration

Though it's vauguely any use in pointer declaration, * is still used. Other use for * is pointer dereferencing.

deallocation is freeing the memory spaces which is done by delete operator or free function.

Question 37

Output

100                                                                                                                           

Sok

Question 38

A structure can't have arrays in it.

both statements

To use a structure, previously it is defined.

A structure can have different types of variables in it.

are true

Question 39

The tag is the name of the structure, and the variables declared inside the braces of the structure are called the members of the structures.

Question 40

We use length() member function String object.

Like this


string txt = “Hello”;

cout << txt.length();


Related Solutions

why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int...
why my code for mergesort always wrong ? void Merge(vector<int>& data, int p, int q, int r) { int n1 = q - p + 1; int n2 = r - q; vector<int>left(n1); vector<int>right(n2); for(int i = 0; i < n1; i++) { left[i] = data[p + i]; } for(int j = 0; j < n2; j++) { right[j] = data[q+j+1]; } int i = 0; int j = 0; for(int k = p; k <= r; k++) { if(left[i]...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Trace the execution of my_recursive_function(100) and my_recursive_function(32) for the following code snippet. [0.5 M] void my_recursive_function(int...
Trace the execution of my_recursive_function(100) and my_recursive_function(32) for the following code snippet. [0.5 M] void my_recursive_function(int n) { if(n == 0) { printf("False"); return; } if(n == 1) { printf("True"); return; } if(n%2==0) my_recursive_function(n/2); else { printf("False"); return; } } int main() { my_recursive_function(n); return 0; }
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)...
Please explain this code line by line void printperm(int *A,int n,int rem,int j) {    if(n==1)       {        for(int k=0;k<j;k++)        cout<<A[k]<<" + ";        cout<<rem<<"\n";        return;       }     for(int i=0;i<=rem;i++)    {          if(i<=rem)          A[j]=i;          printperm(A,n-1,rem-i,j+1);    } }
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i...
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i < len; ++i){ k = Q.dequeue(); Q.enqueue(k);    System.out.println(Q.dequeue()); } } What is the complexity of this code? Select one: a. O(N), N = k b. Q(1) c. Q(N2), N = Q.size() d. O(M), M = Q.size() e. None of the alternatives is correct. which one?
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int...
Part 1.1 Write a function whose prototype is void exchange ( /*inout*/ int *p, /*inout*/ int *q ) that takes two pointers to integer variables and exchanges the values in these variables. Part 1.2 Write a function whose prototype is char lastChar ( /*in*/ const char *str ) that takes a nonempty C-String as parameter and returns the last character in the string. For example the call lastChar ("srjc") will return the character c. Part 1.3 Define an array of...
Explain the code below in details. void accountInfo() { system("cls"); int ch; printf("1-Total Amount Claimed by...
Explain the code below in details. void accountInfo() { system("cls"); int ch; printf("1-Total Amount Claimed by LifeTime Claim Limit subscriber\n"); printf("2-Total number of Annual Claim Limit who have exhausted all their eligible amount\n"); scanf("%d", & ch); if (ch == 1) { int totalAmountClaimedByLifeTimeSubs = 0; for (int i = 0; i < patientCount; i++) { if (patients[i].annualClaim == false) { for (int j = 0; j < patientCount; j++) { if (claims[j].id == patients[i].id) { totalAmountClaimedByLifeTimeSubs += claims[j].amountClaimed; } }...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT