C++ Variables & Data Types MCQ Quiz
Test your knowledge of C++ variables and data types with this interactive quiz. Select the correct answer for each question and see immediate feedback.
Which of the following is a valid variable declaration in C++?
In C++, variable names must begin with a letter or underscore, cannot contain spaces or hyphens, and cannot start with a digit. Option C follows all these rules: starts with an underscore, contains letters and numbers, and no special characters except the underscore.
What is the size of the 'int' data type in C++ on a typical 64-bit system?
On most modern systems including 64-bit systems, the 'int' data type typically occupies 4 bytes (32 bits). However, it's important to note that the C++ standard only specifies minimum sizes for data types, and the actual size can vary between compilers and architectures.
Which data type would be most appropriate for storing a single character in C++?
The 'char' data type is specifically designed to store single characters in C++. It typically occupies 1 byte of memory. While you could technically use other data types like 'int' to store character values, 'char' is the most appropriate and memory-efficient choice for single characters.
Which of the following is NOT a fundamental data type in C++?
The 'string' type is not a fundamental data type in C++. It is a class from the Standard Template Library (STL). The fundamental data types in C++ are: int, float, double, char, bool, void, and variations with modifiers like short, long, signed, and unsigned.
What is the default value of an uninitialized local variable in C++?
Unlike global variables (which are initialized to zero), local variables in C++ are not automatically initialized. They contain whatever value was previously stored in that memory location, which is often referred to as a "garbage value".
Which data type modifier can be used to indicate that a variable should only store non-negative values?
The 'unsigned' modifier is used to indicate that a variable can only store non-negative values. For example, 'unsigned int' can store values from 0 to 4,294,967,295 on a 32-bit system, while a regular 'int' can store both positive and negative values.
What is the range of values that can be stored in a variable of type 'bool' in C++?
The 'bool' data type in C++ is designed to store boolean values, which can only be 'true' or 'false'. While these values are internally represented as 1 and 0 respectively, the valid literal values for a bool variable are only 'true' and 'false'.
Which of the following is the correct way to declare a constant variable in C++?
In C++, the 'const' keyword is used to declare constant variables. Once initialized, the value of a const variable cannot be changed throughout the program. The other options use keywords from different programming languages.
What is the size of the 'double' data type compared to 'float' in C++?
Typically, a 'float' occupies 4 bytes (32 bits) while a 'double' occupies 8 bytes (64 bits). The double data type provides approximately double the precision of float, hence the name "double".
Which of the following is a valid way to initialize multiple variables of the same type in one statement?
In C++, you can declare and initialize multiple variables of the same type in one statement by separating them with commas. Each variable must be individually initialized if you want them all to have the same value. Option A only initializes variable c, option B is invalid syntax, and option D is incorrect syntax.
What is the value of the expression: sizeof('a') in C++?
In C++, character literals like 'a' are of type char, which typically has a size of 1 byte. This is different from C where character literals are of type int. The sizeof operator returns the size in bytes of its operand.
Which data type would be most appropriate for storing a value of 3.14159 with high precision?
The 'double' data type provides higher precision (typically about 15-16 decimal digits) compared to 'float' (typically about 6-7 decimal digits). For storing a value like π (3.14159) with high precision, double is the most appropriate choice among the options given.
C++ I/O & Operators MCQ Quiz
Test your knowledge of C++ Input/Output operations and operators with these additional questions.
Which header file is required for using cin and cout in C++?
The <iostream> header file contains the declarations for the standard input/output stream objects, including cin (standard input) and cout (standard output).
What is the output of the following code?
int x = 5; cout << x++ << " " << ++x;
x++ is post-increment, so it returns the original value (5) then increments x to 6. ++x is pre-increment, so it increments x to 7 first, then returns the new value (7).
Which operator has the highest precedence in C++?
The scope resolution operator (::) has the highest precedence in C++, followed by postfix operators, then prefix operators and unary operators.
What does the following code output?
cout << (10 > 9 > 8);
The expression is evaluated left to right: (10 > 9) which is true (1), then (1 > 8) which is false (0).
Which of the following is used for formatted output in C++?
In C++, formatted output is achieved using cout along with manipulators like setw, setprecision, fixed, etc. from the <iomanip> header.
What is the result of 7 % 2 + 10 / 5 * 3 in C++?
Operator precedence: *, /, % have same precedence (left to right), then +. So: 7 % 2 = 1, 10 / 5 = 2, 2 * 3 = 6, then 1 + 6 = 7.
Which statement correctly reads a whole line of text including spaces?
The getline() function reads a whole line of text including spaces until the newline character. The extraction operator (>>) stops at whitespace.
What is the value of the expression: 5 + 3 * 2 << 1
Operator precedence: * has higher precedence than +, which has higher precedence than <<. So: 3 * 2 = 6, 5 + 6 = 11, then 11 << 1 (bitwise left shift) = 22.
Which of the following is the extraction operator in C++?
The extraction operator (>>) is used with cin to read input from the standard input. The insertion operator (<<) is used with cout for output.
What will be the output?
int a = 5, b = 3; cout << (a == b) << " " << (a != b);
In C++, relational operators return 1 for true and 0 for false. Since a (5) is not equal to b (3), (a == b) is false (0) and (a != b) is true (1).