CPP Objective Questions [Solved With Explanation | Topic wise]
Topic wise C++ MCQ's
SET1:“Data Types”
1. What is the size of wchar_t in C++?a) 2
b) 4
c) 2 or 4
d) based on the number of bits in the system
Answer:d
Explanation: Compiler wants to make CPU as more efficient in accessing the next value.
2. Pick the odd one out
a) array type
b) character type
c) boolean type
d) integer type
Answer:a
Explanation: Array type is not the basic type and it is constructed using the basic type.
3. Which datatype is used to represent the absence of parameters?
a) int
b) short
c) void
d) float
Answer:c
Explanation: void will not return anything.
4. What does a escape code represent?
a) alert
b) backslash
c) tab
d) form feed
Answer:a
Explanation: Because a is used to produce a beep sound.
5. Which type is best suited to represent the logical values?
a) integer
b) boolean
c) character
d) all of the mentioned
Answer:b
Explanation: Logical values can be either true or false, so the boolean type is suited for it.
6. Identify the user-defined types from the following?
a) enumeration
b) classes
c) both a and b
d) int
Answer:c
Explanation:They must be defined by the users before use unlike the other types which are readily available.
7. Which of the following statements are true?
int f(float)
a) f is a function taking an argument of type int and retruning a floating point number
b) f is a function taking an argument of type float and returning a integer.
c) f is a function of type float
d) none of the mentioned
Answer:b
Explanation: The argument that is passed to a function f is of float type and the function finally retruns a value that id is of integer type.
8. The value 132.54 can represented using which data type?
a) double
b) void
c) int
d) bool
Answer:a
Explanation: The given value is with decimal points, so float or double can be used.
9. When a language has the capability to produce new data type mean, it can be called as
a) overloaded
b) extensible
c) encapsulated
d) reprehensible
Answer:b
Explanation: Extensible is used to add new features to C++.
10. Pick the odd one out.
a) integer, character, boolean, floating
b) enumeration, classes
c) integer, enum, void
d) arrays, pointer, classes
Answer:c
Explanation: Option a consists of all fundamental types, option b consists of user-definied types and option d consists of derived types but option c is a mixture.
input/output.
SET 2:"Booleans"
1. Is bool a fundamental datatype in C++?
a) Yes
b) No, it is a typedef of unsigned char
c) No, it is an enum of {false,true}
d) No, it is expanded from macros
Answer:a
Explanation: C++ has bool as a fundamental data type.
2. Find the odd one out:
a) std::vector<int>
b) std::vector<short>
c) std::vector<long>
d) std::vector<bool>
Answer:d
Explanation: std::vector<bool> is a specialized version of vector, which is used for elements of type bool and optimizes for space. It behaves like the unspecialized version of vector and the storage is not necessarily an array of bool values, but the library implementation may optimize storage so that each value is stored in a single bit.
3. What is the value of the bool?
bool is_int(789.54)
a) True
b) False
c) 1
d) none of the mentioned
Answer:b
Explanation: The given number is a double not an integer, so the function returns 0 which is boolean false.
4. What happens when a null pointer is converted into bool?
a) An error is flagged
b) bool value evaluates to true
c) bool value evaluates to false
d) the statement is ignored
Answer:c
Explanation: A pointer can be implicitly converted to a bool. A nonzero pointer converts to true and zerovalued pointer converts to false.
5. Which of the following statements are false?
a) bool can have two values and can be used to express logical expressions.
b) bool cannot be used as the type of the result of the function.
c) bool can be converted into integers implicitly
d) a bool value can be used in arithemetic expressions.
Answer:b
Explanation: None.
6. For what values of the expression is an if-statement block not executed?
a) 0 and all negative values
b) 0 and -1
c) 0
d) 0, all negative values, all positive values except 1
Answer:c
Explanation: The if-statement block is only not executed when the expression evaluates to 0. It’s just syntactic sugar for a branch-if-zero instruction.
7. Which of the two operators ++ and — work for the bool datatype in C++?
a) None
b) ++
c) —
d) Both
Answer:b
Explanation: Due to history of using integer values as booleans, if an integer is used as a boolean, then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it. However, it’s not possible to predict the result of — given knowledge only of the truth value of x, as it could result in false.
8. What is the output of the following program?
#include <iostream>
using namespace std;
int f(int p, int q)
{
if (p > q)
return p;
else
return q;
}
main()
{
int a = 5, b = 10;
int k;
bool x = true;
bool y = f(a, b);
k =((a * b) + (x + y));
cout << k;
}
a) 55
b) 62
c) 52
d) none of the mentioned
Answer:c
Explanation: None.
9. What is the value of p?
#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;
}
a) 0
b) 16
c) 12
d) 2
Answer:b
Explanation: None.
10. Evaluate the following
(false && true) || false || true
a) 0
b) 1
c) false
d) none of the mentioned
Answer:b
Explanation: None.
SET 3:"Character Types"
1. How many characters are specified in the ASCII scheme?
a) 64
b) 128
c) 256
d) none of the mentioned
Answer:b
Explanation: None.
2. Select the right option.
Given the variables p, q are of char type and r, s, t are of int type
1. t = (r * s) / (r + s);
2. t = (p * q) / (r + s);
a) 1 is true but 2 is false
b) 1 is false and 2 is true
c) both 1 and 2 are true
d) both 1 and 2 are false
Answer:c
Explanation: Every character constant has an integer value. Also char belongs to the integral type hence arithmetic and logical operations can be performed on them.
3. Which of the following belongs to the set of character types?
a) char
b) wchar_t
c) only a
d) both a and b
Answer:d
Explanation: wchar_t and char is used to represent wide character and character.
4. What will be the output of this program?
#include <iostream>
using namespace std;
int main()
{
char c = 74;
cout << c;
return 0;
}
a) A
b) N
c) J
d) I
Answer:c
Explanation: The literal value for 74 is J. So it will be printing J.
5. How do we represent a wide character of the form wchar_t?
a) L’a’
b) l’a’
c) L[a] d) la
Answer:a
Explanation: A wide character is always indicated by immediately preceding the character literal by an L.
6. What is the output of this program?
#include <stdio.h>
int main()
{
char a = '\012';
printf("%d", a);
return 0;
}
a) Compiler error
b) 12
c) 10
d) Empty
Answer:c
Explanation: The value ‘\012’ means the character with value 12 in octal, which is decimal 10.
7. In C++, what is the sign of character data type by default?
a) Signed
b) Unsigned
c) Implementation dependent
d) None of these
Answer:c
Explanation: The standard does not specify if plain char is signed or unsigned. There are three distinct character types according to the standard: char, signed char and unsigned char.
8. Is the size of character literals different in C and C++?
a) Implementation defined
b) Can’t say
c) Yes, they are different
d) No, they are not different
Answer:c
Explanation: In C++, sizeof(‘a’) == sizeof(char) == 1. In C however, sizeof(‘a’) == sizeof(int).
9. Suppose in a hypothetical machine, the size of char is 32 bits. What would sizeof(char) return?
a) 4
b) 1
c) Implementation dependent
d) Machine dependent
Answer:b
Explanation: The standard does NOT require a char to be 8-bits, but does require that sizeof(char) return 1.
10. What constant defined in <climits> header returns the number of bits in a char?
a) CHAR_SIZE
b) SIZE_CHAR
c) BIT_CHAR
d) CHAR_BIT
Answer:d
Explanation: None.
SET 4: Integer Types
1. The size_t integer type in C++ is?
a) Unsigned integer of at least 64 bits
b) Signed integer of at least 16 bits
c) Unsigned integer of at least 16 bits
d) Signed integer of at least 64 bits
Answer:c
Explanation: The size_t type is used to represent the size of an object. Hence, it’s always unsigned. According to the language specification, it is at least 16 bits.
2. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
unsigned int y = 2;
if(x > y) {
cout << "x is greater";
} else {
cout << "y is greater";
}
}
a) x is greater
b) y is greater
c) Implementation defined
d) Arbitrary
Answer:a
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.
3. Which of these expressions will return true if the input integer v is a power of two?
a) (v | (v + 1)) == 0;
b) (~v & (v – 1)) == 0;
c) (v | (v – 1)) == 0;
d) (v & (v – 1)) == 0;
Answer:d
Explanation: Power of two integers have a single set bit followed by unset bits.
4. What is the value of the following 8-bit integer after all statements are executed?
int x = 1;
x = x << 7;
x = x >> 7;
a) 1
b) -1
c) 127
d) Implementation defined
Answer:d
Explanation: Right shift of signed integers is undefined, and has implementation-defined behaviour.
5. Which of these expressions will make the rightmost set bit zero in an input integer x?
a) x = x | (x-1)
b) x = x & (x-1)
c) x = x | (x+1)
d) x = x & (x+1)
Answer:b
Explanation: None.
6. Which of these expressions will isolate the rightmost set bit?
a) x = x & (~x)
b) x = x ^ (~x)
c) x = x & (-x)
d) x = x ^ (-x)
Answer:c
Explanation: None.
7. 0946, 786427373824, ‘x’ and 0X2f are _____, _____, ____ and _____ literals respectively
a) decimal, character,octal, hexadecimal
b) octal, hexadecimal, character, decimal
c) hexadecimal, octal, decimal, character
d) octal, decimal, character, hexadecimal
Answer:d
Explanation: Literal integer constants that begin with 0x or 0X are interpreted as hexadecimal and the ones that begin with 0 as octal. The character literal are written within ”.
8. What will be the output of this program?
#include <iostream>
using namespace std;
int main()
{
int a = 8;
cout << "ANDing integer 'a' with 'true' :" << a && true;
return 0;
}
a) ANDing integer ‘a’ with ‘true’ :8
b) ANDing integer ‘a’ with ‘true’ :0
c) ANDing integer ‘a’ with ‘true’ :1
d) None of the mentioned
Answer:a
Explanation: None.
9. What will be output of this program?
#include <iostream>
using namespace std;
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
cout << l << k;
return 0;
}
a) compile time error
b) -1 1
c) 1 -1
d) implementation defined
Answer:b
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.
10. What will be output of this function?
int main()
{
register int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}
a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
Answer:c
Explanation: Using & on a register variable may be invalid, since the compiler may store the variable in a register, and finding the address of it is illegal.
SET 4:"Integer Types"
1. The size_t integer type in C++ is?
a) Unsigned integer of at least 64 bits
b) Signed integer of at least 16 bits
c) Unsigned integer of at least 16 bits
d) Signed integer of at least 64 bits
Answer:c
Explanation: The size_t type is used to represent the size of an object. Hence, it’s always unsigned. According to the language specification, it is at least 16 bits.
2. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int x = -1;
unsigned int y = 2;
if(x > y) {
cout << "x is greater";
} else {
cout << "y is greater";
}
}
a) x is greater
b) y is greater
c) Implementation defined
d) Arbitrary
Answer:a
Explanation: x is promoted to unsigned int on comparison. On conversion x has all bits set, making it the bigger one.
3. Which of these expressions will return true if the input integer v is a power of two?
a) (v | (v + 1)) == 0;
b) (~v & (v – 1)) == 0;
c) (v | (v – 1)) == 0;
d) (v & (v – 1)) == 0;
Answer:d
Explanation: Power of two integers have a single set bit followed by unset bits.
4. What is the value of the following 8-bit integer after all statements are executed?
int x = 1;
x = x << 7;
x = x >> 7;
a) 1
b) -1
c) 127
d) Implementation defined
Answer:d
Explanation: Right shift of signed integers is undefined, and has implementation-defined behaviour.
5. Which of these expressions will make the rightmost set bit zero in an input integer x?
a) x = x | (x-1)
b) x = x & (x-1)
c) x = x | (x+1)
d) x = x & (x+1)
Answer:b
Explanation: None.
6. Which of these expressions will isolate the rightmost set bit?
a) x = x & (~x)
b) x = x ^ (~x)
c) x = x & (-x)
d) x = x ^ (-x)
Answer:c
Explanation: None.
7. 0946, 786427373824, ‘x’ and 0X2f are _____, _____, ____ and _____ literals respectively
a) decimal, character,octal, hexadecimal
b) octal, hexadecimal, character, decimal
c) hexadecimal, octal, decimal, character
d) octal, decimal, character, hexadecimal
Answer:d
Explanation: Literal integer constants that begin with 0x or 0X are interpreted as hexadecimal and the ones that begin with 0 as octal. The character literal are written within ”.
8. What will be the output of this program?
#include <iostream>
using namespace std;
int main()
{
int a = 8;
cout << "ANDing integer 'a' with 'true' :" << a && true;
return 0;
}
a) ANDing integer ‘a’ with ‘true’ :8
b) ANDing integer ‘a’ with ‘true’ :0
c) ANDing integer ‘a’ with ‘true’ :1
d) None of the mentioned
Answer:a
Explanation: None.
9. What will be output of this program?
#include <iostream>
using namespace std;
int main()
{
int i = 3;
int l = i / -2;
int k = i % -2;
cout << l << k;
return 0;
}
a) compile time error
b) -1 1
c) 1 -1
d) implementation defined
Answer:b
Explanation: Sign of result of mod operation on negative numbers is sign of the dividend.
10. What will be output of this function?
int main()
{
register int i = 1;
int *ptr = &i;
cout << *ptr;
return 0;
}
a) 0
b) 1
c) Compiler error may be possible
d) Runtime error may be possible
Answer:c
Explanation: Using & on a register variable may be invalid, since the compiler may store the variable in a register, and finding the address of it is illegal.
SET 5:"Floating Point Types"
1. Which of the following is not one of the sizes of the floating point types?
a) short float
b) float
c) long double
d) double
Answer:a
Explanation:Floating point types occur in only three sizes-float, long double and double.
2. Which of the following is a valid floating point literal?
a) f287.333
b) F287.333
c) 287.e2
d) 287.3.e2
Answer:c
Explanation:To make a floating point literal, we should attach a suffix of ‘f’ or ‘F’ and there should not be any blank space.
3. What is the range of the floating point numbers?
a) -3.4E+38 to +3.4E+38
b) -3.4E+38 to +3.4E+34
c) -3.4E+38 to +3.4E+36
d) -3.4E+38 to +3.4E+32
Answer:a
Explanation:None.
4. Which of three sizes of floating point types should be used when extended precision is required?
a) float
b) double
c) long double
d) extended float
Answer:c
Explanation:Float for single precision, double for double precision and long double for extended precision.
5. What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
float num1 = 1.1;
double num2 = 1.1;
if (num1 == num2)
cout << "stanford";
else
cout << "harvard";
return 0;
}
a) harvard
b) stanford
c) compile time error
d) runtime error
Answer:a
Explanation:Float store floating point numbers with 8 place accuracy and requires 4 bytes of Memory. Double has 16 place accuracy having size of 8 bytes.
Output:
$ g++ float3.cpp
$ a.out
harvard
6. What is the output of this program?
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
cout << setprecision(17);
double d = 0.1;
cout << d << endl;
return 0;
}
a) 0.11
b) 0.10000000000000001
c) 0.100001
d) compile time error
Answer:b
Explantion:The double had to truncate the approximation due to it’s limited memory, which resulted in a number that is not exactly 0.1.
Output:
$ g++ float2.out
$ a.out
0.10000000000000001
7. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
float i = 123.0f;
cout << i << endl;
return 0;
}
a) 123.00
b) 1.23
c) 123
d) compile time error
Answer:c
Explanation:The value 123 is printed because of its precision.
$ g++ float.cpp
$ a.out
123
8. Which is used to indicate single precision value?
a) F or f
b) L or l
c) either a or b
d) neither a or b
Answer:a
Explanation:None.
9. What is the output of this program?
#include <iostream>
using namespace std;
int main()
{
float f1 = 0.5;
double f2 = 0.5;
if (f1 == 0.5f)
cout << "equal";
else
cout << "not equal";
return 0;
}
a) equal
b) not equal
c) compile time error
d) runtime error
Answer:a
Explanation:0.5f results in 0.5 to be stored in floating point representations.
Output:
$ g++ float.cpp
$ a.out
equal
10. Which is correct with respect to size of the datatypes?
a) char > int < float b) int < char > float
c) char < int < float d) char < int < double [expand title=""]
Answer:d
Explanation:The char has lesser bytes than int and int has lesser bytes than double whereas int and float can potentially have same sizes. [/expand]
SET 6:"Floating Point Types"
1. The size of an object or a type can be determined using which operator?
a) malloc
b) sizeof
c) malloc
d) calloc
Answer:b
Explanation:The sizeof operator gives the size of the object or type.
2. It is guaranteed that a ____ has atleast 8bits and a ____ has atleast 16 bits.
a) int, float
b) char, int
c) bool, char
d) char, short
Answer:d
Explanation:None.
3. Implementation dependent aspects about an implementation can be found in ____
a) <implementation>
b) <limits>
c) <limit>
d) <numeric>
Answer:b
Explanation:The limit header holds the details of the machine dependent details.
4. Size of C++ objects are expressed in terms of multiples of the size of a ____ and the size of a char is ____.
a) char, 1
b) int, 1
c) float, 8
d) char, 4
Answer:a
Explanation:None.
5. Identify the incorrect option.
a) 1 <= sizeof(bool) <= sizeof(long) b) sizeof(float) <= sizeof(double) <= sizeof(long double) c) sizeof(char) <= sizeof(long) <=sizeof(wchar_t) d) sizeof(N) = sizeof(signed N) = sizeof(unsigned N) [expand title=""] Answer:c
Explanation:sizeof(char) <= sizeof(wchar_t) <= sizeof(long) [/expand] 6. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num = 0x20 + 020 + 20;
cout << sizeof(num)<<'\n';
return 0;
}
a) 2
b) 4
c) Depends on compiler.
d) garbage
Answer:c
Explanation:The sum of three numbers are belongs to different number systems, so the result is typecasted into integer.
Output:
$ g++ size.cpp
$ a.out
4
7. What is the output of the following program?
#include <iostream>
using namespace std;
int main ( )
{
static double i;
i = 20;
cout << sizeof(i);
return 0;
}
a) 4
b) 2
c) 8
d) garbage
Answer:c
Explanation:The size of the double data type is 8.
$ g++ size1.cpp
$ a.out
8
8. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num1 = 10;
float num2 = 20;
cout << sizeof(num1 + num2);
return 0;
}
a) 2
b) 4
c) 8
d) garbage
Answer:b
Explanation:In this program, integer is converted into float. Therefore the result of num1 and num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp
$ a.out
4
9. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int a = 5;
float b;
cout << sizeof(++a + b);
cout << a;
return 0;
}
a) 2 6
b) 4 6
c) 2 5
d) 4 5
Answer:d
Explanation:The a as a integer will be converted to float while calculating the size. The value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.
Output:
$ g++ size3.cpp
$ a.out
4 5
10. What would be the output of the following program (in 32-bit systems)?
#include <iostream>
using namespace std;
int main()
{
cout << sizeof(char);
cout << sizeof(int);
cout << sizeof(float);
return 0;
}
a) 1 4 4
b) 1 4 8
c) 1 8 8
d) none of the mentioned
Answer:a
Explanation:Character is 1 byte, integer 4 bytes and float 4 bytes.
1. The size of an object or a type can be determined using which operator?
a) malloc
b) sizeof
c) malloc
d) calloc
Answer:b
Explanation:The sizeof operator gives the size of the object or type.
2. It is guaranteed that a ____ has atleast 8bits and a ____ has atleast 16 bits.
a) int, float
b) char, int
c) bool, char
d) char, short
Answer:d
Explanation:None.
3. Implementation dependent aspects about an implementation can be found in ____
a) <implementation>
b) <limits>
c) <limit>
d) <numeric>
Answer:b
Explanation:The limit header holds the details of the machine dependent details.
4. Size of C++ objects are expressed in terms of multiples of the size of a ____ and the size of a char is ____.
a) char, 1
b) int, 1
c) float, 8
d) char, 4
Answer:a
Explanation:None.
5. Identify the incorrect option.
a) 1 <= sizeof(bool) <= sizeof(long) b) sizeof(float) <= sizeof(double) <= sizeof(long double) c) sizeof(char) <= sizeof(long) <=sizeof(wchar_t) d) sizeof(N) = sizeof(signed N) = sizeof(unsigned N) [expand title=""] Answer:c
Explanation:sizeof(char) <= sizeof(wchar_t) <= sizeof(long) [/expand] 6. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num = 0x20 + 020 + 20;
cout << sizeof(num)<<'\n';
return 0;
}
a) 2
b) 4
c) Depends on compiler.
d) garbage
Answer:c
Explanation:The sum of three numbers are belongs to different number systems, so the result is typecasted into integer.
Output:
$ g++ size.cpp
$ a.out
4
7. What is the output of the following program?
#include <iostream>
using namespace std;
int main ( )
{
static double i;
i = 20;
cout << sizeof(i);
return 0;
}
a) 4
b) 2
c) 8
d) garbage
Answer:c
Explanation:The size of the double data type is 8.
$ g++ size1.cpp
$ a.out
8
8. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int num1 = 10;
float num2 = 20;
cout << sizeof(num1 + num2);
return 0;
}
a) 2
b) 4
c) 8
d) garbage
Answer:b
Explanation:In this program, integer is converted into float. Therefore the result of num1 and num2 is float. And it is returning the size of the float.
Output:
$ g++ size2.cpp
$ a.out
4
9. What is the output of the following program?
#include <iostream>
using namespace std;
int main()
{
int a = 5;
float b;
cout << sizeof(++a + b);
cout << a;
return 0;
}
a) 2 6
b) 4 6
c) 2 5
d) 4 5
Answer:d
Explanation:The a as a integer will be converted to float while calculating the size. The value of any variable doesn’t modify inside sizeof operator. Hence value of variable a will remain 5.
Output:
$ g++ size3.cpp
$ a.out
4 5
10. What would be the output of the following program (in 32-bit systems)?
#include <iostream>
using namespace std;
int main()
{
cout << sizeof(char);
cout << sizeof(int);
cout << sizeof(float);
return 0;
}
a) 1 4 4
b) 1 4 8
c) 1 8 8
d) none of the mentioned
Answer:a
Explanation:Character is 1 byte, integer 4 bytes and float 4 bytes.
Comments
Post a Comment