MCQs on Programming Basics

[Set - 3]

1. How many times will the following loop execute? for i in range(5): print(i)

A) 4 times in total
B) 5 times in total
C) Infinite due to error
D) Once for every element

Correct Answer: B) 5 times in total
Explanation: The range(5) function generates numbers from 0 to 4, so the loop executes 5 times, printing each value.

2. What is the output of the following: print(5 // 2) in Python?

A) 2 for integer division
B) 2.5 for floating result
C) 0 due to truncation
D) Error in syntax use

Correct Answer: A) 2 for integer division
Explanation: The // operator performs floor division, discarding the fractional part. Dividing 5 by 2 gives 2.

3. Which keyword is used to define a class in Python?

A) class for structure
B) def for declaration
C) struct for data
D) object for templates

Correct Answer: A) class for structure
Explanation: The class keyword in Python defines a blueprint for creating objects, encapsulating methods and attributes.

4. What is the correct way to initialize a dictionary in Python?

A) (key1:value1, key2:value2)
B) [key1:value1, key2:value2]
C) {key1:value1, key2:value2}
D) <key1:value1, key2:value2>

Correct Answer: C) {key1:value1, key2:value2}
Explanation: Dictionaries in Python use curly braces {} to store key-value pairs, separated by a colon :.

5. What is the use of the is keyword in Python?

A) Assigns values to variables
B) Compares identity of objects
C) Defines scope in a block
D) Checks for membership

Correct Answer: B) Compares identity of objects
Explanation: The is operator checks whether two variables point to the same object in memory, not just their values.

6. What is the significance of indentation in Python code?

A) Indicates block of code
B) Adds comments in loops
C) Separates multiple lines
D) Specifies logical errors

Correct Answer: A) Indicates block of code
Explanation: Python uses indentation to define blocks of code, such as those in loops, functions, and conditionals, instead of braces.

7. Which of the following is a valid function definition in Python?

A) define my_function() {pass}
B) function my_function(): pass
C) def my_function(): pass
D) func my_function {return;}

Correct Answer: C) def my_function(): pass
Explanation: Python functions are defined using the def keyword, followed by the function name, parentheses, and a colon.

8. What is the output of the following: 5 == 5.0 in most programming languages?

A) False because types differ
B) Cannot evaluate condition
C) Error in type coercion
D) True because values match

Correct Answer: D) True because values match
Explanation: In most languages, 5 (integer) and 5.0 (float) are considered equal because their values are equivalent.

9. Which of the following is the correct syntax to create a 2D array in Java?

A) int[][] arr = new int[3][3];
B) int arr[3][3] = new int;
C) int arr = [3][3] integers;
D) int new array[3][3];

Correct Answer: A) int[][] arr = new int[3][3];
Explanation: In Java, a 2D array is declared using double square brackets [][] followed by memory allocation with new.

10. What is the difference between break and return statements in a function?

A) break continues execution; return halts all code
B) break exits loops; return exits functions
C) break skips lines; return changes logic
D) break modifies loops; return accesses variables

Correct Answer: B) break exits loops; return exits functions
Explanation: The break statement exits a loop, while return exits a function and optionally returns a value.

11. Which of the following is used to declare a variable in JavaScript?

A) let declaration keyword
B) def declaration keyword
C) init declaration keyword
D) var declaration keyword

Correct Answer: D) var declaration keyword
Explanation: In JavaScript, var and let are commonly used to declare variables, with let being the modern preference.

12. Which keyword is used to define a block of code that might throw an error?

A) error for debug tools
B) catch for conditions
C) try for error handling 
D) block for secure access

Correct Answer: C) try for error handling
Explanation: The try keyword is used to define a block of code where exceptions might occur, typically followed by catch or finally.

13. What is the correct way to find the size of an array in most programming languages?

A) array.length property
B) size(array) function
C) array.count property
D) getSize(array) function

Correct Answer: A) array.length property
Explanation: The .length property is widely used in many programming languages to find the number of elements in an array.

14. How are private variables typically indicated in object-oriented programming?

A) By starting with an underscore _
B) By using all-uppercase names
C) By ending with a semicolon ;
D) By using angle brackets < >

Correct Answer: A) By starting with an underscore _
Explanation: In many languages like Python, private variables are indicated by a single or double underscore prefix, though true enforcement depends on the language.

15. Which of the following is a mutable data type in Python?

A) Float is immutable
B) String is immutable
C) Tuple is immutable
D) List is mutable type

Correct Answer: D) List is mutable type
Explanation: Lists in Python can be modified after creation, while strings, tuples, and numbers are immutable.

16. Which symbol is used to start a single-line comment in Python?

A) ;; for single-line comment
B) // for single-line comment
C) # for single-line comment
D) ** for single-line comment

Correct Answer: C) # for single-line comment
Explanation: In Python, single-line comments start with a # symbol, and the interpreter ignores the text following it.

17. What is the output of not True in Python?

A) Error in logical syntax
B) True is the output
C) Null for invalid logic
D) False is the output

Correct Answer: D) False is the output
Explanation: The not operator inverts a boolean value. Since True is inverted, the result is False.

18. How do you declare a multi-line string in Python?

A) “”” for multiple lines
B) <<EOF for multiple lines
C) {} brackets for strings
D) using backticks “

Correct Answer: A) “”” for multiple lines
Explanation: Multi-line strings in Python are enclosed in triple quotes (""" or '''), allowing the string to span multiple lines.

19. Which function is used to iterate over a sequence in Python?

A) for is used in Python
B) iter() is used in Python
C) range() is used in Python
D) map() is used in Python

Correct Answer: B) iter() is used in Python
Explanation: The iter() function creates an iterator object, allowing sequential access to elements in a collection.

20. What is the significance of the pass statement in Python?

A) Skips a specific iteration
B) Terminates the program
C) Restarts the current loop
D) Defines an empty block

Correct Answer: D) Defines an empty block
Explanation: The pass statement in Python acts as a placeholder for code that is syntactically required but not yet implemented.

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.