C++ MCQs with explanation

TOKENS, EXPRESSIONS AND CONTRAL STRUCTURES

1. Which one of the following is an ANSI C keyword?

Explanation ≡ Correct answer: option d) volatile
The keyword "volatile" is an ANSI C keyword used to indicate that a variable's value can be changed at any time outside the code's control, such as by an external hardware device or an interrupt service routine.

2. Which one of the following is not a C++ keyword?

Explanation ≡ Correct answer: option b) false
The keyword "false" is not a C++ keyword. C++ does not have a built-in keyword "false" or "true" like some other programming languages. Instead, C++ uses boolean literals "true" and "false" to represent true and false values respectively.

3. Which of the following statements is TRUE with regard to naming C++ identifiers?

Explanation ≡ Correct answer: option a) A name can start with any letter or digit.
In C++, an identifier is a name given to a variable, function, or other user-defined entity. Identifiers can start with any letter (uppercase or lowercase) or underscore, followed by any combination of letters, digits, and underscores.

4. Which of the following is a valid C++ identifier?

Explanation ≡ Correct answer: option c) _string1
A valid C++ identifier can start with a letter (uppercase or lowercase) or an underscore. It can be followed by any combination of letters, digits, and underscores. Option c) _string1 is a valid identifier, while option b) 2 x is not valid because it starts with a digit.

5. Which of the following is not a valid literal constant in C++?

Explanation ≡ Correct answer: option c) 12, 345
Option c) 12, 345 is not a valid literal constant in C++. A valid integer literal can consist of digits without commas. The commas in 12, 345 make it an invalid literal. Options a), b), and d) are all valid literal constants in C++.

6. Which of the following is a user-defined data type in C++?

Explanation ≡ Correct answer: option d) function
Functions are user-defined data types in C++. When you define a function, you are creating a new data type that represents a specific operation or computation. Functions can be called and used throughout the program to perform that operation whenever needed.

7. Which of the following is not a built-in data type in C++?

Explanation ≡ Correct answer: option a) structure
In C++, "structure" is not a built-in data type. Instead, it is a user-defined data type used to group different variables of possibly different data types under a single name. Options b), c), and d) are all built-in data types in C++.

8. Which of the following program segments is valid in C++?

Explanation ≡ Correct answer: option c) void *p1; char *p2; p2 = (char *) p1;
In C++, when assigning a pointer of one type to another pointer of a different type, you need to use a type cast. In this case, p1 is a void pointer, and p2 is a char pointer. To assign p1 to p2, you need to use a type cast as shown in option c).

9. Assuming that we are working on a 16-bit word machine, which of the following takes a size of 8 bytes?

Explanation ≡ Correct answer: option d) double
On a 16-bit word machine, a double data type typically takes 8 bytes. Options a), b), and c) are all larger data types that would take more than 8 bytes on a 16-bit word machine.

10. Which of the following declaration statements is illegal?

Explanation ≡ Correct answer: option b) const int count;
In C++, a constant variable must be initialized at the time of its declaration. Option b) const int count; does not provide an initial value for the constant variable "count," making it an illegal declaration.

11. Which of the following statements is FALSE?

Explanation ≡ Correct answer: option d) A variable must be initialized at the time of its declaration.
In C++, a variable can be declared without initialization. If a variable is not initialized at the time of declaration, it will contain garbage data. However, it is good practice to initialize variables to avoid unexpected behavior.

12. Identify the error, if any, in the statement: long sum = m1 + m2;

Explanation ≡ Correct answer: option b) Initialization at the time of declaration is not valid.
The given statement "long sum = m1 + m2;" is a valid declaration and initialization of a long integer variable named "sum." There is no error in the statement.

13. Which of the following statements is invalid?

Explanation ≡ Correct answer: option d) int *p = new int[m][10];
The statement "int *p = new int[m][10];" is invalid because the "new" operator can only allocate memory for one-dimensional arrays in C++. It cannot allocate memory for a two-dimensional array directly as shown in option d). To allocate memory for a two-dimensional array, you would need to use a pointer to a pointer or a dynamic array of pointers.

14. For using the manipulator setw(), we must include the header file:

Explanation ≡ Correct answer: option c)
The manipulator "setw()" is used to set the width of the output field when printing data in C++. To use "setw()", you need to include the header file .

15. The following statements implement the type cast operation. Which one of them is illegal?

Explanation ≡ Correct answer: option d) x = y/float m;
Option d) x = y/float m; is illegal because it is missing the parentheses around "float m". The correct syntax for a type cast is "(type)expression", but in this case, the parentheses are missing, making it an invalid statement.

16. The statement a=(b=10) – 5; is an example of

Explanation ≡ Correct answer: option a) Chained assignment
The statement "a=(b=10) – 5;" is an example of chained assignment. The value of "b" is assigned the value 10 first, and then the value of "a" is assigned the value of "b" (which is 10) minus 5, resulting in "a" being assigned the value 5.

17. Which one of the following expressions is an example of a bitwise expression?

Explanation ≡ Correct answer: option c) x << 10
The expression "x << 10" is an example of a bitwise expression. The "<<" operator performs a bitwise left shift operation on the value of "x" by 10 positions. Bitwise operators manipulate individual bits of integers, rather than treating the whole value as a single unit like arithmetic operators.

18. Which of the following operators in C++ can be overloaded?

Explanation ≡ Correct answer: options a) and b) - Conditional operator (?:) and Scope resolution operator (::)
In C++, the conditional operator (?:) and the scope resolution operator (::) can be overloaded. Overloading allows you to define multiple behaviors for an operator when used with different data types or operands.

19. In which of the following groups, all the operators are in the same level of precedence?

Explanation ≡ Correct answer: option d) %, *, ->*, /=
In option d), all the operators (%, *, ->*, /=) are at the same level of precedence. Precedence refers to the order in which operators are evaluated when an expression contains multiple operators. Operators with higher precedence are evaluated before operators with lower precedence.

20. Which of the following loop structures is known as an exit-controlled loop?

Explanation ≡ Correct answer: option c) for loop
The for loop is an exit-controlled loop in C++. It repeats a set of statements while a given condition is true, and it exits the loop when the condition becomes false. The for loop consists of an initialization, a condition, and an update expression, making it ideal for exit-controlled looping.


Your Score is 10

THE STUDENT FRIENDLY BOOK TO LEARN C++. 
Let us C++ by Famous author Yashavant Kanetkar
Indian writer


Topic wise C++ MCQs Index ≡ (Click to open)
Click on particular topic to visit that page
  1. Principles of Object-Oriented Programming 
  2. Beginning with C++
  3. Tokens, Expressions, and Control Structures
  4. Functions in C++
  5. Classes and Objects
  6. Constructors and Destructors
  7. Operator Overloading
  8. Inheritance
  9. Pointers, Virtual Functions, and Polymorphism
  10. Managing Console I/O Operations
  11. Working with Files
  12. Templates
  13. Exception Handling
  14. Standard Template Library
  15. Manipulating Strings
  16. New Features in ANSI C++ Standards
  17. Object-Oriented System Development

Post a Comment

0 Comments