C++ Practice Test Quiz!

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Mohzozo1900
M
Mohzozo1900
Community Contributor
Quizzes Created: 1 | Total Attempts: 2,904
Questions: 22 | Attempts: 2,907

SettingsSettingsSettings
C++ Practice Test Quiz! - Quiz

.


Questions and Answers
  • 1. 

    The basic commands that a computer performs are ____, and performance of arithmetic and logical operations.

    • A.

      Input, file, list

    • B.

      Input, output, storage

    • C.

      Output, folder, storage

    • D.

      Storage, directory, log

    Correct Answer
    B. Input, output, storage
    Explanation
    The basic commands that a computer performs are input, output, and storage. Input refers to the process of receiving data or instructions from external sources, such as a keyboard or mouse. Output involves displaying or transmitting data or results to external devices, such as a monitor or printer. Storage refers to the ability of a computer to store and retrieve data and instructions for later use. These three commands are fundamental to the functioning of a computer system.

    Rate this question:

  • 2. 

    A program called a(n) ____ translates instructions written in high-level languages into machine code.

    • A.

      Assembler

    • B.

      Decoder

    • C.

      Compiler

    • D.

      Linker

    Correct Answer
    C. Compiler
    Explanation
    A compiler is a program that takes instructions written in high-level languages and translates them into machine code. It analyzes the entire program, checking for errors and translating the code into a form that can be understood by the computer's processor. Unlike an assembler, which translates assembly language into machine code, a compiler works with higher-level languages like C, C++, or Java. A decoder is not responsible for translating high-level languages, and a linker is used to combine object files into an executable program. Therefore, a compiler is the correct answer for this question.

    Rate this question:

  • 3. 

    Suppose that x is an int variable. Which of the following expressions always evaluates to true?

    • A.

      (x > 0) || ( x <= 0)

    • B.

      (x > 0) && ( x <= 0)

    • C.

      (x >= 0) || (x == 0)

    • D.

      (x > 0) && (x == 0)

    Correct Answer
    A. (x > 0) || ( x <= 0)
    Explanation
    The expression (x > 0) || (x <= 0) will always evaluate to true for any integer x. This is because the logical OR (||) operator ensures that if at least one of the conditions is true, the entire expression is true. Since any integer x will either be greater than 0 or less than or equal to 0, this condition covers all possible integer values.
    The other expressions have conditions that can never be true simultaneously (such as x > 0 and x <= 0 both being true at the same time), so they do not always evaluate to true.

    Rate this question:

  • 4. 

    Which of the following expressions correctly determines that x is greater than 10 and less than 20?

    • A.

      10 < x < 20

    • B.

      (10 < x < 20)

    • C.

      10 < x || x < 20

    • D.

      10 < x && x < 20

    Correct Answer
    D. 10 < x && x < 20
    Explanation
    The expression "10 < x && x < 20" correctly determines that x is greater than 10 and less than 20. The "&&" operator is the logical AND operator, which means both conditions must be true for the overall expression to be true. In this case, the first condition "10 < x" checks if x is greater than 10, and the second condition "x < 20" checks if x is less than 20. Only if both conditions are true, the expression will be true, indicating that x is within the desired range.

    Rate this question:

  • 5. 

    What is the output of the following C++ code? int x = 35; int y = 45; int z; if (x > y) z = x + y; else z = y – x; cout << x << " " << y << " " << z << endl;

    • A.

      35 45 80

    • B.

      35 45 –10

    • C.

      35 45 10

    • D.

      35 45 0

    Correct Answer
    C. 35 45 10
    Explanation
    The code initializes variables x and y with the values 35 and 45 respectively. It then checks if x is greater than y. Since 35 is not greater than 45, the else block is executed. In the else block, z is assigned the value of y minus x, which is 45 - 35 = 10. Finally, the values of x, y, and z are printed, resulting in the output "35 45 10".

    Rate this question:

  • 6. 

    Suppose that x, y, z, and w are int variables, and x = 3, y = 4, z = 7, and w = 1; What is the output of the following statement?  cout << " x == y : " << (x == y ) << endl;

    • A.

      X == y: 0

    • B.

      X != z : 1

    • C.

      Y == z – 3: 1

    • D.

      ! (z > w): 0

    Correct Answer
    A. X == y: 0
    Explanation
    The output of the statement is "x == y: 0". This is because the expression (x == y) evaluates to false, since the value of x (3) is not equal to the value of y (4). In C++, when a comparison is true, it is represented as 1, and when it is false, it is represented as 0. Therefore, the output is 0.

    Rate this question:

  • 7. 

    Suppose that x, y, z, and w are int variables, and x = 3, y = 4, z = 7, and w = 1; What is the output of the following statement?  cout << " ! (z > w ) : " << ! (z > w ) << endl;

    • A.

      X != z : 1

    • B.

      X == y: 0

    • C.

      Y == z – 3: 1

    • D.

      ! (z > w): 0

    Correct Answer
    D. ! (z > w): 0
    Explanation
    The output of the statement is " ! (z > w) : 0". This is because the expression "(z > w)" evaluates to true (1) since 7 is greater than 1. The "!" operator negates the result, so it becomes false (0). The statement then outputs the string " ! (z > w) : " followed by the result of the expression, which is 0.

    Rate this question:

  • 8. 

    The type of error in the following statement is : int a=b/0     //  b is defined as int

    • A.

      Syntax error

    • B.

      Logoical error

    • C.

      Run-time error

    • D.

      Compilation error

    Correct Answer
    C. Run-time error
    Explanation
    The given statement will result in a run-time error because it is trying to divide a number by zero. In programming, dividing by zero is not allowed and will cause an error during the execution of the program.

    Rate this question:

  • 9. 

    The driver function of a C++ program that if it is not present in a program, no execution can take place: 

    • A.

      Main

    • B.

      #include directive

    • C.

      Using namespace

    • D.

      Std

    Correct Answer
    A. Main
    Explanation
    The driver function of a C++ program is the main function. It is the entry point of the program where the execution starts. Without the main function, the program cannot be executed as there would be no starting point for the program to begin execution. Therefore, if the main function is not present in a C++ program, no execution can take place.

    Rate this question:

  • 10. 

    What is the output of the following code : int main() { int a=4 ; (a>0)? cout<< " Number is positive " : cout<< " Number is negative " ; return 0 ; }

    • A.

      True

    • B.

      False

    • C.

      Number is positve

    • D.

      Number is negative

    Correct Answer
    C. Number is positve
    Explanation
    The output of the code will be "Number is positive". This is because the code uses the ternary operator "?:" to check if the value of variable "a" is greater than 0. If it is true, it will print "Number is positive", otherwise it will print "Number is negative". In this case, since the value of "a" is 4 which is greater than 0, the condition is true and "Number is positive" will be printed.

    Rate this question:

  • 11. 

    What is the output of the following code : int main() { int num = 9; if (num<0); num= -num ; return o ; }

    • A.

      9

    • B.

      -9

    • C.

      0

    • D.

      Syntax error

    Correct Answer
    D. Syntax error
    Explanation
    The key points to note are:
    The if (num < 0); line has a semicolon at the end, which terminates the if statement. This means that the subsequent line num = -num; will execute regardless of the if condition.
    As a result, num will be assigned the value -num, making num negative.
    However, there is a mistake in the return statement: return o; should be return 0;. Assuming this is a typographical error and should be corrected to return 0;, the program will compile and run, but since there are no cout statements to display output, the program's output will not show any value in a typical run.

    Rate this question:

  • 12. 

    The output of the following code is : int main() { int n=17 ; cout << ( n == 16 ) << endl ; return 0 ; }

    • A.

      N=16

    • B.

      0

    • C.

      (n=16)

    • D.

      (n==16)

    Correct Answer
    B. 0
    Explanation
    The code is checking if the value of n is equal to 16. Since n is initialized as 17, the condition (n == 16) evaluates to false. The false value is then printed to the console using the cout statement. In C++, false is represented as 0. Therefore, the output of the code is 0.

    Rate this question:

  • 13. 

    A C++ code line ends with? 

    • A.

      A Semicolon (;)

    • B.

      A full stop (.)

    • C.

      A comma ( ,)

    • D.

      Slash (/)

    Correct Answer
    A. A Semicolon (;)
    Explanation
    In C++, a code line is terminated with a semicolon (;). This is a fundamental rule in the C++ programming language. The semicolon is used to indicate the end of a statement or expression. It is necessary to include a semicolon at the end of each line of code to ensure proper syntax and to separate different statements or expressions within the code.

    Rate this question:

  • 14. 

    What is the output of the following code: int main() { int x=7 ; if ( x=8)   cout << x-1 ; else  cout << x+1 ; return 0 ; }

    • A.

      8

    • B.

      6

    • C.

      7

    • D.

      Syntax error

    Correct Answer
    C. 7
    Explanation
    The output of the code is 7. In the if statement, the condition x=8 is used instead of x==8. This is an assignment operator, not a comparison operator. As a result, the value of x is set to 8. Since the condition is true, the code inside the if statement is executed, which is cout << x-1. Therefore, the output is 7.

    Rate this question:

  • 15. 

    What is the output of the following code: int main () { int x=2 , y ; //x=x*2 ; y=x++ ; cout << "x= " << x << "\ty="  << y ; return 0 ; }

    • A.

      X = 4 y = 5

    • B.

      X = 3 y = 2

    • C.

      X = 2 y= 3

    • D.

      X = 5 y = 4

    Correct Answer
    B. X = 3 y = 2
    Explanation
    The code starts by initializing the variable x to 2. Then, the line "y = x++" assigns the value of x to y and then increments the value of x by 1. After that, the code prints the values of x and y using the "cout" statement. Since x was incremented by 1 before printing, its value is 3. The value of y remains 2 because it was assigned the value of x before it was incremented. Therefore, the output of the code is "x = 3 y = 2".

    Rate this question:

  • 16. 

    What is the output of the following code: int main() { int a=98 ; double b=5 ; cout << a/3.0 ; return 0 ; }

    • A.

      32.6667

    • B.

      32

    • C.

      33

    • D.

      32.66

    Correct Answer
    A. 32.6667
    Explanation
    In the expression a/3.0, the value of a is an integer (98) and the value of 3.0 is a double. When performing arithmetic operations with operands of different types, C++ promotes the operands to the highest data type involved in the operation, which in this case is double. Therefore, the division operation will be performed using double arithmetic, resulting in the decimal value 32.6667.

    Rate this question:

  • 17. 

    Which of the following is a unary operator? 

    • A.

      *

    • B.

      ++

    • C.

      %

    • D.

      /

    Correct Answer
    B. ++
    Explanation
    The correct answer is ++. A unary operator is an operator that operates on a single operand. In this case, the ++ operator is a unary operator because it increments the value of the operand by 1.

    Rate this question:

  • 18. 

    Which of the following is not a relational operator in the C++ program? 

    • A.

      =

    • B.

      ==

    • C.

      >=

    • D.

      <=

    Correct Answer
    A. =
    Explanation
    The equal sign (=) is not a relational operator in C++. It is actually an assignment operator used to assign a value to a variable. Relational operators are used to compare values and return a boolean result, such as whether one value is equal to another (==) or greater than or equal to another (>=).

    Rate this question:

  • 19. 

    C++ program may have more than one main function? 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    A C++ program cannot have more than one main function. The main function is the entry point of the program, and there can only be one entry point. Having multiple main functions would lead to ambiguity and confusion in the program's execution. Therefore, it is false that a C++ program may have more than one main function.

    Rate this question:

  • 20. 

    Which of the following is not a standard data type ? 

    • A.

      Int

    • B.

      Char

    • C.

      Float

    • D.

      Data

    Correct Answer
    D. Data
    Explanation
    The given options include three standard data types: int, char, and float. However, "data" is not a standard data type in most programming languages. It is a generic term that can refer to any type of information, but it is not a specific data type like int, char, or float. Therefore, "data" is the correct answer as it is not a standard data type.

    Rate this question:

  • 21. 

    The output of the following code is : int main() { float x=1 ; cout << ( x == 3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0 ) ; return 0 ; }

    • A.

      1.0

    • B.

      0.999999

    • C.

      True

    • D.

      None of above

    Correct Answer
    D. None of above
    Explanation
    The output of the code will be "false" or "0". This is because the expression (3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0) evaluates to a value slightly less than 1.0 due to the limitations of floating-point precision. When comparing this value to the float variable x, which is initialized to 1.0, the result is false. Therefore, the correct answer is "none of above".

    Rate this question:

  • 22. 

    Which of the following is illegal identifier ?  

    • A.

      EmoName

    • B.

      X2

    • C.

      X_Y

    • D.

      Return

    Correct Answer
    D. Return
    Explanation
    The identifier "return" is illegal because it is a reserved keyword in many programming languages, including C++, Java, and Python. Reserved keywords are predefined and have special meanings in the language, so they cannot be used as identifiers for variables, functions, or other entities in the program.

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Aug 10, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Oct 24, 2017
    Quiz Created by
    Mohzozo1900
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.