MCQs on Programming Basics

[Set - 1]

1. What is the result of the following code snippet: x = 5; y = x + 2; print(y)?

A) 2 is displayed on the screen
B) 5 is displayed as the result
C) 7 is displayed on the screen
D) Error due to missing semicolon

Correct Answer: C) 7 is displayed on the screen
Explanation: The code assigns 5 to x, adds 2 to it, and assigns the result to y. When y is printed, the output is 7.

2. Which of the following is used to terminate a while loop in most programming languages?

A) break statement
B) continue statement
C) pass statement
D) next statement

Correct Answer: A) break statement
Explanation: The break statement is used to exit a loop immediately, regardless of the loop’s condition.

3. Which keyword is used to declare a constant in programming?

A) static keyword
B) let keyword
C) const keyword
D) fixed keyword

Correct Answer: C) const keyword
Explanation: The const keyword is used to declare constants, ensuring the variable’s value cannot be changed after initialization.

4. What is the output of the expression: 10 % 3?

A) 0 as the result
B) 1 as the remainder
C) 3 as the divisor
D) 10 as the numerator

Correct Answer: B) 1 as the remainder
Explanation: The modulo operator % returns the remainder of a division. Dividing 10 by 3 gives a remainder of 1.

5. Which of the following is NOT a valid data type in most programming languages?

A) float data type
B) boolean data type
C) number data type
D) string data type

Correct Answer: C) number data type
Explanation: Most programming languages do not have a number data type; they use specific types like int, float, or double.

6. What is the purpose of the elif statement in programming?

A) Terminate a program’s execution
B) Provide additional conditional checks
C) Repeat a block of code continuously
D) Initialize variables within a loop

Correct Answer: B) Provide additional conditional checks
Explanation: The elif statement allows for additional conditions to be checked when the initial if condition is false.

7. In a for loop, which part specifies the range of values to iterate over?

A) Condition to terminate
B) Initialization variable
C) Range function or sequence
D) Increment or decrement step

Correct Answer: C) Range function or sequence
Explanation: In a for loop, the range function or sequence defines the set of values over which the loop will iterate.

8. Which operator is used for logical AND in most programming languages?

A) || symbol for logic
B) && symbol for logic
C) == symbol for comparison
D) ++ operator for increment

Correct Answer: B) && symbol for logic
Explanation: The && operator checks whether both conditions in a logical statement are true.

9. What happens if a variable is used without being initialized in most languages?

A) An error is thrown immediately
B) The variable is treated as null
C) A default value is assigned
D) Undefined behavior occurs

Correct Answer: D) Undefined behavior occurs
Explanation: In most languages, using an uninitialized variable can lead to unpredictable behavior or errors, depending on the language.

10. How is a block of code defined in Python?

A) Using curly braces {}
B) Indenting lines properly
C) Adding a semicolon ;
D) Using parentheses ()

Correct Answer: B) Indenting lines properly
Explanation: Python uses indentation to define blocks of code, unlike other languages that rely on braces or keywords.

11. What is the default value of an uninitialized integer variable in most languages like C/C++?

A) 0 is assigned by default
B) A random value is assigned
C) Null is used for uninitialized variables
D) Undefined behavior occurs

Correct Answer: B) A random value is assigned
Explanation: In C/C++, uninitialized variables may hold a garbage value unless explicitly initialized.

12. What will be the result of the following code snippet: x = 5; x += 2; print(x)?

A) 2 is printed
B) 5 is printed
C) 7 is printed
D) Error due to syntax

Correct Answer: C) 7 is printed
Explanation: The x += 2 statement is shorthand for x = x + 2, so x becomes 7, which is then printed.

13. Which of the following loops guarantees execution at least once?

A) for loop in general
B) while loop in all cases
C) do-while loop in logic
D) nested loop in recursion

Correct Answer: C) do-while loop in logic
Explanation: A do-while loop executes its code block at least once before checking the condition.

14. What is the purpose of the continue statement in a loop?

A) Skip the current iteration
B) Restart the loop from the beginning
C) Terminate the loop execution
D) Reverse the iteration sequence

Correct Answer: A) Skip the current iteration
Explanation: The continue statement skips the remaining code in the current iteration and proceeds to the next loop cycle.

15. Which function is commonly used to get the length of a string or array?

A) size() function for arrays
B) length() function for objects
C) len() function for sequences
D) count() function for elements

Correct Answer: C) len() function for sequences
Explanation: In languages like Python, the len() function returns the number of elements in strings, arrays, or lists.

16. How is a comment written in C++?

A) Using # symbol at the beginning
B) Using // for single-line comments
C) Wrapping text with {} braces
D) Using : at the start

Correct Answer: B) Using // for single-line comments
Explanation: Single-line comments in C++ begin with //, while multi-line comments are enclosed within /* */.

17. What will the expression True and False evaluate to in Python?

A) True for both conditions
B) Error in boolean comparison
C) Null for invalid logic
D) False because both are not true 

Correct Answer: D) False because both are not true
Explanation: The and operator evaluates to True only if both conditions are True. Since one is False, the result is False.

18. In a function, what is the purpose of the return statement?

A) End the function and return a value
B) Restart the function execution
C) Skip some lines in the function
D) Log the results for debugging

Correct Answer: A) End the function and return a value
Explanation: The return statement exits a function and optionally passes a value back to the caller.

19. What is the primary difference between a list and an array in programming?

A) Arrays store fixed types; lists can mix types
B) Lists are faster than arrays in memory
C) Arrays handle larger data automatically
D) Lists encrypt elements by default

Correct Answer: A) Arrays store fixed types; lists can mix types
Explanation: In most languages, arrays store elements of a fixed type, while lists can hold mixed types and are more dynamic.

20. Which of the following is a valid identifier in most programming languages?

A) 123VariableName as identifier
B) _myVarName for consistency
C) ifKeywordName for readability
D) $Var-Name as declaration

Correct Answer: B) _myVarName for consistency
Explanation: Identifiers must begin with a letter or underscore and cannot include reserved keywords or special characters like $ or -.

Share this with your friends

Solve More MCQs on Programming Basics

Show All
Lorem Ipsum is simply dumy text of the printing typesetting industry lorem ipsum.

Subscribe to our Newsletter

Subscribe to our Newsletter

& stay ahead of your competitors!

You have been successfully Subscribed! Ops! Something went wrong, please try again.
Subscribe Form

* Subscribe to our email newsletter to get the latest posts delivered right to your email.

© MCQs 4 Exams. All rights reserved.