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; }
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
#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); }
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
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; }
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
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);    } }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT