This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1What 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;
}
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: 2What is the output of code?
void main()
{
int x=33, y,z;
z=x++ + 17;
y=++z - ++x;
cout<<”x”<<x<<”y”<<y<<”z”<<z;
}
Option B
Initially, the value of x is 33. In the statement z = x++ + 17;, the post-increment operator x++ first uses the current value of x and then increases it by 1. Therefore, 33 is used in the expression and afterward x becomes 34. So, the value of z becomes 33 + 17 = 50.
Next, in the statement y = ++z - ++x;, both ++z and ++x are pre-increment operators. Therefore, the values are increased before being used in the expression. So, z becomes 51 and x becomes 35. Now calculate, y=51-35=16.
The final values are x = 35, y = 16, z = 51.
Q: 3The output of the following statements are
int a=5;
cout<<"FIRST"<<(a<<2)<<"SECOND";
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: 4What will be the output of the following?
int a=5;
cout<<”FIRST”<<(a<<2)<<”SECOND”;
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: 5Find output of the following program?
void main()
{
int a=23;
cout<<"Hello",
cout<<a;
}
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: 6What 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);
}
}
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: 7In C++, the statement sizeof(‘a’); returns?
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: 8Which statement is NOT correct regarding ternary operator in C++?
Option A
The ternary operator in C++ is a conditional operator used to make decisions in a compact form. It works similarly to the if-else statement.
Syntax:
E.g.:
The ternary operator is also called the conditional operator because it checks a condition before selecting a value. However, operator overloading is not possible for the ternary operator ?: in C++.
Q: 9Consider the following C++ program. What is the output of this program?
int main()
{
int sal;
sal=4+2*15;
cout<<sal;
return 0;
}
Option B
In C++, expressions are evaluated according to operator precedence (priority), where multiplication (*) has higher priority than addition (+).
In the given statement sal=4+2*15;, the multiplication is performed first, so 2*15 = 30, and then this result is added to 4, giving 4+30 = 34. Therefore, the value stored in sal is 34, and this is printed as the output.
Q: 10In C++, operator >> is used along with object cin for reading input is known as _________.
Option B
In C++, the >> operator is used with the cin object to take input from the keyboard. This operator is called the get from or extraction operator because it extracts data from the input stream and stores it into variables.
Q: 11In C++, the statement sizeof(254.78); returns?
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: 12Which 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:
You have reached the end of this topic. Continue learning with the next topic below.
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.