1.
What is the output of the following snippet?
int a=0101;
int b=101;
cout<<a<<” , ”<<b;
Correct Answer
C. Compilation Error
Explanation
The code snippet will result in a compilation error. This is because the numbers starting with a leading zero (like 0101) are considered as octal numbers in C++. However, the number 101 is not a valid octal number as it contains the digit 8. Therefore, the compiler will throw an error.
2.
What is the output of the following code ?
int i = 10;
int &r; r = &i;
cout<<i<<":"<<r;
Correct Answer
B. Compilation Error
Explanation
The given code will result in a compilation error. This is because the statement "int &r; r = &i;" is trying to create a reference variable "r" without initializing it. In C++, reference variables must be initialized when they are declared. Therefore, the code will not compile.
3.
What is the output of the following snippet ?
class Test
{
};
int main()
{
Test t;
cout<<sizeof(t);
return 0;
}
Correct Answer
B. 1
Explanation
The output of the given snippet is 1. This is because the sizeof operator is used to determine the size in bytes of an object or data type. In this case, sizeof(t) is used to determine the size of the object 't' of type Test. Since the Test class does not have any member variables, its size is 1 byte.
4.
Which of the following way is the correcy way to set a value 3.14 in a variable pi such that it can not be modified ?
Correct Answer
C. Const float pi = 3.14F;
Explanation
The correct way to set a value of 3.14 in a variable pi such that it cannot be modified is by using the "const" keyword. By declaring the variable as "const float pi = 3.14F;", it ensures that the value of pi remains constant and cannot be changed throughout the program. This prevents any accidental or intentional modifications to the value of pi.
5.
What is the output of the following snippet ?
unsigned int a = 5;
int b=2;
int status = (a>b)?1:0;
cout<<status;
Correct Answer
B. 0
Explanation
The output of the snippet is 0. This is because the expression "(a > b)" evaluates to false since 5 is not greater than 2. Therefore, the value of "status" is assigned as 0. The "cout" statement then outputs the value of "status", which is 0.
6.
What is the output of the following snippet ?
int *p=10;
cout<<*p;
Correct Answer
C. Compilation Error
Explanation
The given code snippet will result in a compilation error. This is because the variable 'p' is declared as a pointer to an integer, but it is being assigned the value 10 directly instead of the address of an integer variable. This is an invalid assignment and will cause a compilation error.
7.
What is the output of the following code snippet ?
int main()
{
int a=1, b=2, c=3,d,e;
d=a,b,c;
e=(a,b,c);
cout<<d<<e;
Correct Answer
B. 1,3
Explanation
The output of the code snippet is 1,3. In the line "d=a,b,c;", the comma operator is used. The comma operator evaluates all the expressions and returns the value of the last expression. So, d is assigned the value of c, which is 3. In the line "e=(a,b,c);", the comma operator is also used. However, in this case, the comma operator evaluates all the expressions but returns the value of the second expression. So, e is assigned the value of b, which is 1. Finally, the values of d and e are printed, resulting in 1,3.
8.
What is the output of the following snippet ?
int a=5.6;
float b=5;
cout<<a<<”\t”<<b;
Correct Answer
C. 5,5 and warning
Explanation
The output of the snippet is "5, 5 and warning". The variable 'a' is declared as an integer but assigned a floating-point value, which results in a warning. The value of 'a' is truncated to 5. The variable 'b' is declared as a float and assigned an integer value, which is valid. The output statement prints the values of 'a' and 'b' separated by a tab character.
9.
What is the output of the following snippet ?
double a=10.5;
double b=4.0
cout<<a%b<<” “<<a*b;
cout<<” “<<a+b<<” “<<a-b;
Correct Answer
B. Compilation Error
Explanation
The given code snippet will result in a compilation error. The reason for this is that the modulus operator (%) cannot be used with double data types. The modulus operator can only be used with integer data types. Therefore, the statement "cout
10.
What is the output of the following snippet ?
char* const p="abc";
cout<<p;
p=“x”;
cout<<p;
Correct Answer
D. Compilation Error
Explanation
The given code snippet will result in a compilation error. This is because the variable "p" is declared as a constant pointer to a character, meaning that the pointer itself cannot be changed to point to a different memory location. Therefore, the assignment "p = "x";" will give a compilation error.
11.
What is the output of the following snippet ?
int main()
{
int a=10;
{
a=20;
{
a=30;
cout<<a;
}
cout<<a;
}
cout<<a;
}
Correct Answer
B. 30,30,30
Explanation
The output of the snippet is 30, 30, 30.
This is because the variable 'a' is assigned the value of 10 initially. Then, within the innermost block, 'a' is reassigned the value of 30. This value is then printed within that block. After that, the value of 'a' is still 30 when it is printed again within the outer block. Finally, when 'a' is printed outside of any block, it is still 30. Therefore, the output is 30, 30, 30.
12.
What is the output of the following snippet ?
cout<<sizeof(NULL)<<" "<<sizeof("");
Correct Answer
C. 8,1
Explanation
The output of the snippet is "8,1". The sizeof(NULL) returns the size of a pointer, which is typically 8 bytes on a 64-bit system. The sizeof("") returns the size of an empty string, which is 1 byte (for the null terminator character).
13.
What is the output of the following snippet ?
int a(2);
int b=2;
cout<<a<<”\t”<<b;
Correct Answer
A. 2,2
Explanation
The output of the given snippet is "2 2". The variable "a" is initialized with the value 2, and the variable "b" is also assigned the value 2. The "cout" statement prints the values of "a" and "b" separated by a tab character.
14.
What is the output of the following snippet ?
int main()
{
cout<<a;
return 0;
}
int a=10;
Correct Answer
D. Compilation Error
Explanation
The code snippet will result in a compilation error. This is because the variable "a" is declared and initialized after the cout statement. In C++, variables must be declared before they are used. Therefore, the compiler will throw an error indicating that "a" is not declared in the scope of the cout statement.
15.
What is the Output of the following code ?
int a=1,b=2;
a^=b^=a^=b;
cout<<a<<" "<<b;
Correct Answer
C. 2,1
Explanation
The code uses the XOR operator (^) to swap the values of variables a and b.
First, a^=b^=a^=b is evaluated as a^=b^=1^=b. Since b is initially 2, b^=1^=2 is evaluated as b^=3. This means that b is updated to 1.
Next, a^=1^=2 is evaluated as a^=3. Since a is initially 1, this means that a is updated to 2.
Therefore, the final values of a and b are 2 and 1 respectively.
16.
What is the output of the following snippet ?
int a = 5+NULL;
cout<<a;
Correct Answer
A. 5
Explanation
The output of the given snippet is 5. When adding a NULL value to an integer, it is treated as 0. Therefore, 5 + NULL is equivalent to 5 + 0, resulting in 5.
17.
What is the output of the following snippet ?
float a=3.5;
a++;
char c='A';
c++;
int i=-2;
i++;
cout<<a<<"\t"<<c<<"\t"<<i;
Correct Answer
C. 4.5 B -1
Explanation
The output of the given snippet is "4.5 B -1".
- The variable "a" is initialized with the value 3.5 and then incremented by 1 using the post-increment operator. So, "a" becomes 4.5.
- The variable "c" is initialized with the character 'A' and then incremented by 1 using the post-increment operator. The ASCII value of 'A' is 65, so after incrementing, "c" becomes 'B'.
- The variable "i" is initialized with the value -2 and then incremented by 1 using the post-increment operator. So, "i" becomes -1.
- Finally, the values of "a", "c", and "i" are printed using the cout statement.
18.
What is the output of the following snippet ?
int main()
{
int a=1;
cout<<a+++++a;
}
Correct Answer
C. Compilation Error
Explanation
The given code snippet will result in a compilation error. This is because the expression "a+++++a" is ambiguous and not valid in C++. The compiler cannot determine the correct meaning of this expression, leading to a compilation error.
19.
What is the output of the following snippet ?
#define SQUARE(X) (X*X)
int main()
{
cout<<SQUARE(5);
cout<<SQUARE(2+3);
}
Correct Answer
B. 25,13
Explanation
The output of the snippet is 25, 13.
The macro SQUARE(X) is defined as (X*X), so when SQUARE(5) is called, it is replaced with (5*5), which evaluates to 25.
Similarly, when SQUARE(2+3) is called, it is replaced with (2+3*2+3), which evaluates to 2+9, resulting in 11.
Therefore, the output is 25, 13.
20.
What is the output of the following snippet?
int a=10;
cout<<a++*a++<<”\t”;
cout<<a++*++a<<”\t”;
Correct Answer
B. 100,169
Explanation
The output of the first statement is 100 because the post-increment operator increases the value of 'a' to 11 and then multiplies it by the original value of 'a' which is 10. The output of the second statement is 169 because the first post-increment operator increases 'a' to 11, then the pre-increment operator increases it to 12, and then it is multiplied by the original value of 'a' which is 12.
21.
What is the output of the following snippet ?
cout<<2/9<<"\t";
cout<<2.0/9<<"\t";
cout<<2/9.0<<"\t";
cout<<2.0/9.0<<"\t";
Correct Answer
C. 0 0.2 0.2 0.2
Explanation
The first cout statement outputs the result of integer division 2/9, which is 0. The second cout statement outputs the result of floating-point division 2.0/9, which is approximately 0.2222. The third cout statement outputs the result of floating-point division 2/9.0, which is approximately 0.2222. The fourth cout statement outputs the result of floating-point division 2.0/9.0, which is approximately 0.2222. Therefore, the correct answer is 0 0.2 0.2 0.2.
22.
What is the output of the following snippet ?
short int a=1;
a=a<<15;
cout<<a;
Correct Answer
A. -32768
Explanation
The output of the given snippet is -32768. In this code, the variable 'a' is initially assigned the value 1. Then, the bitwise left shift operator '
23.
What is the output of the following snippet ?
int array[10] = {2,1,4,5};
int &r = array[2];
cout<<*r;
Correct Answer
B. Compilation Error
Explanation
The code snippet declares an array of integers with a size of 10 and initializes the first four elements with the values 2, 1, 4, and 5. It then creates a reference variable 'r' that refers to the third element of the array. Finally, it prints the value pointed to by 'r', which is 4. Therefore, the correct answer is 4. There is no compilation error in this code.
24.
What is the output of the following code snippet ?
int a=1,b=2;
int x=!!a;
int y=!a&&b;
cout<<x<<”\t”<<y;
Correct Answer
B. 1 0
Explanation
The code snippet initializes two variables, "a" with the value 1 and "b" with the value 2. Then, it assigns the value of the logical NOT operator applied twice to "a" to the variable "x". This means that "x" will be 1, as the logical NOT operator returns true (1) if the operand is false (0), and false (0) if the operand is true (non-zero). Next, it assigns the value of the logical AND operator applied to the logical NOT of "a" and "b" to the variable "y". Since "a" is true (non-zero), the logical NOT of "a" is false (0), and the logical AND operator returns false (0) if any of its operands are false (0). Therefore, "y" will be 0. Finally, the code prints the values of "x" and "y" separated by a tab character. So, the output will be "1 0".
25.
What is the output of the following code snippet ?
int a=1,b=1;
int x=(a<2)&&(b=100);
int y=(a==b)&&(a!=b);
cout<<x<<”\t”<<y<<”\t”<<a<<”\t”<<b;
Correct Answer
D. 1 0 1 100
Explanation
The code snippet initializes two variables, a and b, with the value 1.
In the line "int x=(a
26.
What is the output of the following code snippet ?
int a=-10;
cout<<!(~a);
Correct Answer
A. 0
Explanation
The code snippet initializes the variable 'a' with the value -10. The bitwise complement operator '~' inverts the bits of 'a', resulting in 9. The logical negation operator '!' is then applied to 9, which converts any non-zero value to 0. Therefore, the output of the code is 0.
27.
What is the output of the following code snippet ?
int a=-10, b=-22;
cout<<a++<<a++<<b++<<b++;
Correct Answer
B. -9 -10 -21 -22
Explanation
The code snippet uses the post-increment operator (a++) and the stream insertion operator (
28.
What is the output of the following code snippet ?
int a=10;
cout<<++a*a++<<”\t”<<++a*++a;
Correct Answer
A. 169 144
Explanation
The code snippet uses the pre-increment and post-increment operators on the variable 'a'. In the first part of the code, '++a' increments 'a' to 11 and then 'a++' returns the value 11 but increments 'a' to 12. So, the expression '++a*a++' becomes 11 * 11 which is 121. In the second part of the code, '++a' increments 'a' to 13 and then '++a' increments 'a' to 14. So, the expression '++a*++a' becomes 13 * 14 which is 182. Therefore, the output of the code snippet is 169 144.
29.
What is the output of the following snippet ?
if(cout<<"hello")
{
cout<<"hi";
}
Correct Answer
B. Hellohi
Explanation
The output of the snippet is "hellohi". This is because the first "cout" statement prints "hello", and since it is within the "if" condition, the second "cout" statement is also executed and prints "hi". Therefore, the output is the combination of both strings, "hello" and "hi".
30.
What is the output of the following code snippet ?
int a;
for(a=0;a++;a++);
cout<<a;
Correct Answer
A. 1
Explanation
The code snippet initializes the variable 'a' as 0. Then, it enters a for loop where the condition 'a++' is checked. However, since the increment operation is placed after the semicolon, the condition will always be false and the loop will not execute. Therefore, the value of 'a' remains 0. Finally, the value of 'a' is printed, which is 0.
31.
What is the output of the following code snippet ?
if(int i=1)
i++;
else
i--;
cout<<i;
Correct Answer
C. Compilation Error
Explanation
The code snippet will result in a compilation error. This is because the if statement requires a boolean expression inside the parentheses, but instead, the code snippet has an initialization statement for the variable i.
32.
What is the output of the following code snippet ?
class ABC
{
int i;
public:
ABC(int i = 0)
{
this.i = i;
}
void display()
{
cout<<i;
}
}
int main()
{
ABC obj(10);
obj.display();
}
Correct Answer
A. Compilation Error
Explanation
The code snippet will result in a compilation error. This is because the line "this.i = i;" is not a valid syntax in C++. The correct syntax to assign a value to a member variable in a constructor is "this->i = i;". Since the code has a compilation error, it will not produce any output.
33.
What is the output of the following code snippet ?
int i;
for(i=1;i<=10;i++,cout<<i<<endl);
Correct Answer
B. 2..11
Explanation
The code snippet is a for loop that initializes an integer variable i to 1, checks if i is less than or equal to 10, increments i by 1, and prints the value of i followed by a new line. The loop continues until i is no longer less than or equal to 10. Therefore, the output will be a sequence of numbers from 2 to 11, each on a new line.