Q: 1 What is the value of ‘p’ in the following C++ code snippet?
#include<iostream>
using namespace std;
int main()
{
int p;
bool a=true;
bool b=false;
int x=10;
int y=5;
p=((x|y)+(a+b));
cout<<p;
return 0;
}
12
16
2
None of the above
[ Option B ]
In this code, the expression (x | y) performs a bitwise OR on 10 and 5.
10 in binary : 1010
5 in binary : 0101
Bitwise OR : 1111 which is 15.
Next, (a + b) adds the Boolean values:
true : 1
false : 0
So, a + b = 1.
Finally, p = 15 + 1 = 16.
Q: 2 The output of the following statements are
int a=5;
cout<<"FIRST"<<(a<<2)<<"SECOND";
FIRST20SECOND
SECOND25FIRST
SECOND8FIRST
FIRST52SECOND
[ Option A ]
In C++, the symbol << is used in two ways: it works as an output operator (also called the insertion or put-to operator) when used with cout, and as a bitwise left shift operator when it appears between two numeric values.
A bitwise left shift moves the bits of a number to the left by a certain number of positions. In the statement cout << "FIRST" << (a << 2) << "SECOND";, the part (a << 2) is calculated like this, since a = 5, its binary form is 0000 0101. When we shift the bits two places to the left, it becomes 0001 0100, which equals 20 in decimal. So, (a << 2) gives the value 20.
Finally, when substituted into the output statement, it becomes cout << "FIRST" << 20 << "SECOND";, which displays the text FIRST20SECOND on the screen. Note that there are no spaces unless explicitly added.
Q: 3 What will be the output of the following?
int a=5;
cout<<”FIRST”<<(a<<2)<<”SECOND”;
FIRST52SECOND
FIRST20SECOND
SECOND25FIRST
None of the above
[ Option B ]
In this code, the left shift operator << is applied to the integer a, which has the value 5. The expression a << 2 shifts the bits of 5 two positions to the left. In binary, 5 is 00000101, and shifting it left by 2 positions gives 00010100, which is 20 in decimal.
Then, the cout statement prints "FIRST", followed by the result of a<<2, and then "SECOND". As a result, the output of the program is FIRST20SECOND.
Q: 4 Find output of the following program?
void main()
{
int a=23;
cout<<"Hello",
cout<<a;
}
Error occurred, missing semicolon in line cout<<"Hello",
Hello23
Program behave unexpectedly
23Hello
[ Option B ]
The comma operator allows multiple expressions to be written in a single statement. Expressions are evaluated from left to right and the result of the last expression is considered, but all expressions are executed.
The statement cout<<"Hello", prints Hello and cout<<a; prints the value of a, which is 23.
Even though there is no semicolon after cout<<"Hello", it does not cause an error because both statements are combined into one statement using the comma operator.
Q: 5 What will be the output of the following program fragment?
int i=10;
void main()
{
int i=20;
{
int i=30;
printf(“%d,%d”,i,::i);
}
}
30, 10
30, 20
20, 30
None of the above
[ Option A ]
In C++, variables declared in inner scopes (blocks, functions) hide outer scope variables with the same name. The scope resolution operator :: accesses the global variable from any nested scope. In statement printf(“%d,%d”,i,::i);
Q: 6 In C++, the statement sizeof(‘a’); returns?
2
1
3
4
[ Option B ]
The operator sizeof is used to find the size (in bytes) of a data type or an expression.
In C++, a character literal like 'a' is treated as a char type. Since the size of char in C++ is always 1 byte, the expression sizeof('a') returns 1.
Note:
In C, a character literal such as 'a' is treated as an int type, not as a char. Therefore, when the sizeof operator is applied to 'a' in C, it returns the size of an int, which is usually 2 bytes (16-bit compiler) or 4 bytes (32-bit compiler).
Q: 7 In C++, the statement sizeof(254.78); returns?
4
2
8
10
[ Option C ]
In C++, a numeric value written with a decimal point is treated as a floating-point constant. By default, a floating-point literal without any suffix (f or l) is of type double. The size of a double data type is 8 bytes.
Q: 8 Which of the following is a logical operator in C++?
&&
|
~
&
[ Option A ]
In C++, logical operators are used to combine or negate boolean expressions or conditions to evaluate true or false. The logical operators in C++ are:
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.