MCQs on Programming Basics

[Set - 6]

1. What is the difference between a variable and a constant in programming?

A) Constants never change values
B) Variables always store strings
C) Constants allow re-assignment
D) Variables only hold numbers

Correct Answer: A) Constants never change values
Explanation: Variables can store data that changes during program execution, while constants hold fixed values that cannot be altered after initialization.

2. Which symbol is used to end a statement in C, C++, and Java?

A) Colon (:) for ending lines
B) Semicolon (;) for statement
C) Comma (,) for line breaks
D) Period (.) for termination

Correct Answer: B) Semicolon (;) for statement
Explanation: In C, C++, and Java, the semicolon ; marks the end of a statement, making it syntactically complete.

3. How do you declare a variable as an integer in Python?

A) Directly assign value
B) Use the let keyword
C) Declare int variable
D) Add int before value

Correct Answer: A) Directly assign value
Explanation: Python is dynamically typed, so variables are created by directly assigning a value, and their type is inferred.

4. What is the output of the expression 3 * (4 + 5) in most programming languages?

A) Error in expression
B) 12 with no priority
C) 20 with wrong syntax
D) 27 after operations

Correct Answer: D) 27 after operations
Explanation: The parentheses ensure 4 + 5 is evaluated first, and the result is multiplied by 3, giving 27.

5. Which of the following represents the logical NOT operator in most programming languages?

A) ^ symbol for inversion
B) ! symbol for negation
C) & symbol for condition
D) # symbol for logic

Correct Answer: B) ! symbol for negation
Explanation: The ! operator inverts the truth value of a boolean expression, making True become False and vice versa.

6. What is the significance of variable initialization in programming?

A) Reserves memory space
B) Sets an initial value
C) Ensures variables are static
D) Declares variable data type

Correct Answer: B) Sets an initial value
Explanation: Initialization assigns an initial value to a variable, ensuring it doesn’t contain unpredictable (garbage) data.

7. What is the output of the expression 10 / 3 in a programming language that supports floating-point division?

A) 3 with integer truncation
B) 3.0 for simplified result
C) 3.33 for exact division
D) 10 for unexpected logic

Correct Answer: C) 3.33 for exact division
Explanation: Floating-point division retains the decimal part, so dividing 10 by 3 results in approximately 3.33.

8. Which keyword is used to exit a loop prematurely in most programming languages?

A) exit for ending function
B) break to terminate loop
C) continue for skipping step
D) return to exit program

Correct Answer: B) break to terminate loop
Explanation: The break keyword immediately exits the loop, stopping further iterations, regardless of the loop condition.

9. How are multi-line comments written in JavaScript?

A) Enclose with /* */
B) Start lines with ##
C) Use double quotes ""
D) Use triple slashes ///

Correct Answer: A) Enclose with /* */
Explanation: In JavaScript, multi-line comments are written between /* and */, allowing developers to comment multiple lines of code.

10. Which data type is used to represent decimal numbers in Python?

A) str for textual data
B) int for whole numbers
C) float for real numbers
D) list for collections

Correct Answer: C) float for real numbers
Explanation: The float data type in Python is used to represent decimal or real numbers with a fractional part.

11. What is the result of the expression 5 >= 5 in programming?

A) Null due to no condition
B) False for exact match
C) True for equal values
D) Error in syntax usage

Correct Answer: C) True for equal values
Explanation: The >= operator checks if the left-hand operand is greater than or equal to the right-hand operand, which is True in this case.

12. How do you check the length of a string in JavaScript?

A) string.length property
B) string.size method
C) string.count function
D) string.total property

Correct Answer: A) string.length property
Explanation: In JavaScript, the length property is used to determine the number of characters in a string.

13. What happens when a division by zero is performed in most programming languages?

A) Returns infinity as result
B) Raises an error message
C) Produces a zero output
D) Crashes the entire program

Correct Answer: B) Raises an error message
Explanation: Most programming languages raise a runtime error or exception when attempting to divide by zero, as it’s mathematically undefined.

14. Which keyword is used to declare a function in JavaScript?

A) return for function body
B) def for defining code
C) lambda for expressions
D) function for definition

Correct Answer: D) function for definition
Explanation: The function keyword is used to declare functions in JavaScript, followed by the function name and parameters.

15. What is the purpose of a default case in a switch statement?

A) Handle unmatched conditions
B) Exit the program entirely
C) Restart the case evaluation
D) Throw errors on no match

Correct Answer: A) Handle unmatched conditions
Explanation: The default case in a switch statement executes when none of the specified case conditions are met.

16. How do you round a floating-point number to the nearest integer in Python?

A) float(number) method
B) math.ceil() for rounding
C) int(number) for nearest
D) round(number) function 

Correct Answer: D) round(number) function
Explanation: The round() function rounds a floating-point number to the nearest integer, based on the decimal part.

17. What does the term "type inference" mean in programming?

A) Enforcing strict data rules
B) Automatically detecting type
C) Casting between data types
D) Specifying types explicitly

Correct Answer: B) Automatically detecting type
Explanation: Type inference allows a programming language to deduce the type of a variable from its value without explicit declaration.

18. How do you check if a string contains a specific substring in Python?

A) Use in keyword directly
B) Use has() for existence
C) Use contains() method
D) Use search() function

Correct Answer: A) Use in keyword directly
Explanation: In Python, the in keyword checks if a substring exists within a string, returning True or False.

19. Which operator is used to check if two variables refer to the same object in Python?

A) is for object identity
B) == for value equality
C) and for logical check
D) not for inverse logic

Correct Answer: A) is for object identity
Explanation: The is operator checks if two variables refer to the same object in memory, not just equal values.

20. What is the purpose of the "finally" block in exception handling?

A) Execute code regardless of exception
B) Skip the error-handling block
C) Retry the code causing error
D) Raise an exception manually

Correct Answer: A) Execute code regardless of exception
Explanation: The finally block ensures that code runs whether an exception is raised or not, often used for cleanup operations.

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.