Free Programming Basics MCQs with Answers
183 Programming Basics MCQs from Computer Science and IT, each with the correct answer and a written explanation of why it is correct. Free and unlimited, with no account needed.
Programming basics covers algorithms, flowcharts, pseudocode, variables, constants, data types, operators, input and output, conditional statements and loops. It also introduces functions, arrays, strings, debugging and common programming errors, with attention to the difference between syntax errors, runtime errors and logical errors in a program.
Last updated
183 questions · page 5 of 10
- A. It prints "Press the alarm!" and then the computer's speaker beeps
- B. It prints "Press the alarm! \a" literally
- C. A compiler error
- D. The behavior depends on the specific terminal or console being used
Explanation: \a: The alert (bell) escape sequence. In some environments, it produces an audible beep. In others, it might have no effect or display a visual symbol
Correct answer: The behavior depends on the specific terminal or console being used- A. Hello World!
- B. Hexadecimal characters: H e l l o W o r l d !
- C. \x48\x65\x6C\x6C... (and so on)
- D. Compiler error or warning
Explanation: \xHH represents a hexadecimal escape sequence. Here, the codes correspond to the ASCII values of the characters "Hello World!".
Correct answer: Hello World!- A. 1276345
- B. 12763\045
- C. 12[some strange symbol]3
- D. Compiler warning or unpredictable behavior
Explanation: \763: Octal escape sequence representing a character (likely a non-printable one).\0: The null terminator, marking the end of the string. Hence, the output stops before printing '45'.
Correct answer: 12[some strange symbol]3- A. The program will crash
- B. The phrase "and it cannot be changed!" will be deleted
- C. The terminal might produce a beep sound after printing the value of x
- D. No visible effect; the \a will be ignored
Explanation: The behavior of \a (alert/bell) is system-dependent. In many terminals, it produces a beep or a sound alert. However, some modern terminals might simply ignore it.
Correct answer: The terminal might produce a beep sound after printing the value of x- A. f = 3.14, i = 3, c = *
- B. f = 3.14, i = 3, c = (some non-printable character)
- C. f = 3.14, i = 3, c = '3'
- D. Compiler warning or error
Explanation: • (int) f: Truncates the decimal part of f, storing 3 in i.• (char) i: Converts the integer 3 into its corresponding ASCII character. The ASCII value of 3 likely represents a non-printable control character.
Correct answer: f = 3.14, i = 3, c = (some non-printable character)- A. 32768
- B. -32768
- C. -32767
- D. Undefined behavior
Explanation: • short int has a limited range. On most systems, its maximum value is 32767.• Adding 1 causes an integer overflow, resulting in a wrap-around to the minimum representable value, -32768
Correct answer: -32768- A. 2.85
- B. 2.86
- C. 3.00
- D. 2.00
Explanation: Integer division would result in 2. However, since z is a double, the expression is implicitly converted to a floating-point calculation, giving the more precise result
Correct answer: 2.86- A. Positive
- B. Negative
- C. Compiler warning
- D. Undefined behavior
Explanation: x + y involves an unsigned and a signed integer. The signed integer is implicitly converted to unsigned.-10 when represented in unsigned form becomes a very large positive value due to two's complement representation, making the result of x + y positive
Correct answer: Positive- A. 2.00
- B. 2.50
- C. 2.75
- D. 3.00
Explanation: (float) x: Explicitly casts the integer x to a float.The division then becomes a floating-point division, resulting in the precise answer, 2.50
Correct answer: 2.50- A. a = 4, b = 4, result = -1
- B. a = 4, b = 5, result = 1
- C. a = 4, b = 4, result = 0
- D. a = 5, b = 5, result = 0
Explanation: • --a decrements a to 4 first.• b-- uses the current value of b (5) in the subtraction, and then decrements b to 4.• Hence, 4 - 5 = -1.
Correct answer: a = 4, b = 4, result = 0- A. x = 8, y = 1
- B. x = 8, y = -1
- C. x = 9, y = 0
- D. Undefined behavior
Explanation: Modifying the same variable twice within an expression, particularly when using decrement operators, can lead to undefined behavior due to potential side effects and variations in how compilers handle the order of evaluation
Correct answer: Undefined behavior- A. x = -2, y = 1
- B. x = -1, y = 0
- C. x = -2, y = 2
- D. Undefined behavior
Explanation: Again, attempting to modify x multiple times between sequence points leads to undefined behavior, meaning the compiler could produce different results
Correct answer: Undefined behavior- A. x = 0
- B. x = -1
- C. x = -2
- D. Undefined behavior
Explanation: Modifying a variable this many times within a single expression leads to undefined behavior, where different compilers might produce different results
Correct answer: Undefined behavior- A. x = 7, y = 42
- B. x = 6, y = 36
- C. x = 7, y = 36
- D. The output is undefined due to the order of evaluation
Explanation: The order in which the increment operators and multiplication are applied is not guaranteed by the C standard. Different compilers might produce different results, making the behavior undefined
Correct answer: The output is undefined due to the order of evaluation- A. x = 12, y = 22
- B. x = 12, y = 23
- C. x = 11, y = 21
- D. The output is undefined
Explanation: Modifying a variable twice within the same expression, especially when using increment/decrement operators, leads to undefined behavior due to potential side effects and order of evaluation differences across compilers
Correct answer: The output is undefined- A. x = 4, y = 8
- B. x = 3, y = 6
- C. x = 4, y = 6
- D. Compiler error
Explanation: This is a classic example of the post-increment behavior.2 * x++ uses the current value of x (which is 3) in the multiplication and then increments x to 4
Correct answer: x = 4, y = 6- A. i = 1
- B. i = 2
- C. The output is undefined
- D. Compiler error
Explanation: The code attempts to modify i multiple times between sequence points, leading to undefined behavior. The compiler is free to interpret the order of these operations in different ways
Correct answer: The output is undefined- A. a = 4, b = 1, result = 8
- B. a = 4, b = 1, result = 10
- C. a = 5, b = 2, result = 10
- D. The output is undefined
Explanation: --a decrements a to 4 first.b-- uses the current value of b (2) in the multiplication and then decrements b to 1
Correct answer: a = 4, b = 1, result = 10- A. x = 13, y = 33
- B. x = 13, y = 36
- C. x = 10, y = 30
- D. Undefined behavior
Explanation: The order of evaluating the post-increment operators and their side effects is not strictly defined. The result could vary across different compilers.
Correct answer: Undefined behavior- A. 5 5 6
- B. 6 5 6
- C. 5 6 6
- D. Undefined behavior
Explanation: While there's a post-increment, the order of evaluation within the printf statement is well-defined from left to right:1. The first x uses its current value (5).2. x++ increments x to 6 but returns the old value (5).The last x uses the newly incremented value (6).
Correct answer: 5 5 6