1). What is the output of the following code fragment in C++ ? (assumption: all #include and the rest of the code are correct)
int myInt1 = 3, myInt2 = 3;
int *pnt1 = &myInt1, *pnt2 = &myInt2;
myInt1 = ++(*pnt1) + (*pnt1);
myInt2++;
myInt2 = (*pnt2) + (*pnt2);
cout<< myInt1<<myInt2<<endl;
A) 86
B) 78
C) 88
D) 77
Explanation: 
myInt1 = ++(*pnt1) + (*pnt1); // we first pre increment the value of pnt1 (since C++11 this behavior is not
considered undefined) and then we add the values .
myInt2++; // sets the value of myInt2 to 4
myInt2 = (*pnt2)++ + (*pnt2); // pnt2 is a pointer to myInt2 (so when we dereference it returns 4 – the value
of myInt2)

2). What is the output of the following code fragment in C++
void SetElements( int index, int **array, int value = 0)
{
 (*array)[*(&index)] = value;
};
Int main(){
 int *point1, *point2;
 point1 = new int[1];
 point2 = new int[2];
 *point1 = 0;
 SetElements(*&*point1, &point2);
 point1[0] = 1;
 SetElements(*&*point1, &point2, *point1 );
 cout<< point2[(*point1)]<<point2[(*point1)-1]<<endl;
 delete[] point1;
 delete[] point2;
}
A) 11
B) 10
C) 01
D) 00
Explanation: Because SetElement function – sets the element from position “index” of the array “array” to value “value”: array[index ] =value. The first call sets the “point2[0]” to 0 and the second call sets ” point2[1]” to 1

3). Which of the function call will generate a compiler error?
void f()
{}
void f(int)
{}
void f(float)
{}
void f ( float, int = 0)
{}
int main()
{
 short myShort;
 int myInt;
 unsigned int myUInt;
 float myFloat;
 double myDouble;
 f(myShort);
 f(myInt);
 f(myFloat);
 f(myUInt);
 f(myDouble);
}
A) f(myShort)
B) f(myInt)
C) f(myFloat)
D) f(myUInt)
E) f(myDouble)
Explanation:C,D,E :
C – the call “f(myFloat)” is ambiguous to the compiler( two functions match: “f(float)” and “f(float, int =0)”
D,E – the compiler you cannot convert “uint” to “int” and “double” to” float”

4). The values of the following variables are a = ....., b = ....., c=..... .
They are.... memory leaks in the program.
void ModifyVariables(int a, int &b, int *c)
{
 a = b;
 b+=a;;
 c = new int(b);
 (*c)++;
}
int main()
{
 int a=0,b=1, *c;
 c = new int(2);
 ModifyVariables(a,b,c);
 cout<<a<<b<<*c;
 delete c;
}

The values for the following variables are a = 0, b = 2, c = 2. They are 1 memory leaks
Explanation:
The variable “a” is transmitted by value ( there is made a copy of the value of variable “a” so in the expression “a =b ” is modified the value of the local variable “a” ( from the function). 
The variable “b” is transmitted by reference and therefore his values is modified. Even the variable “c” is a pointer , the pointer value itself is transmitted by value, so when you want “c” to point to another memory zone actually the copy made to the “c” pointer points to a new memory zone.
There is one memory leak because you are allocating memory in the “ModifyVariables“ function that is never
freed.

5). What does the following code fragment in C++ do? (assumption: all #include and the rest of the code are correct)
void Pointer(int *p)
{
 (*p)++;
 Reference(*p);
 cout<<*p;
}
void Reference( int &p)
{
 p++;
 Value(p);
 cout<<p;
}
void Value (int p)
{
 p++;
 cout<<p;
}
int main()
{
 int value =3;
 Pointer(&value);
 cout<<value;
}
A) 6555
B) 6565
C) 5656
D) 6565
Explanation: The “Value” function parameter is copied by value (so any changes made by the function to the variable are only local) The rest of the functions modify the content of the initial variable.

6). What does the following code fragment in C++ display? (assumption: all #include and the rest of the code are correct)
int main()
{
 int *index;
 int * vector;
 int i =128;
 vector = new int[5];
 index =vector;
 while (i){
 if (i%2 == 0)
 *index = i;
 i/=2;
 if ((vector + 4) == index )
 break;
 index++;
 }
 cout<<vector[i%5]<<endl;
 delete[] vector;
}
A) 8
B) 16
C) 128
D) 64
Explanation: When we declare “new int[5]” the compiler allocates 5 consecutive blocks of memory of size int. “vector” points to the first element of the allocated memory block. “index++” goes to the next element from the block of memory (the next int) so “index+4” is the fifth element of the vector. 
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: