MCQs on Programming Basics

[Set - 2]

1. What is the correct syntax to declare an array in Java?

A) int[] arr = new int[5];
B) array int arr = 5;
C) declare int arr[5];
D) int arr = [5];

Correct Answer: A) int[] arr = new int[5];
Explanation: In Java, arrays are declared using square brackets [], followed by the new keyword to allocate memory for the specified size.

2. How can you access the last element of an array in most languages?

A) Use array[length-1] for access
B) Reference array[-1] directly
C) Access array[length*2] directly
D) Use array[0] for the last element

Correct Answer: A) Use array[length-1] for access
Explanation: Arrays are zero-indexed in most languages, so the last element is accessed using the index length-1.

3. What is the default value of a boolean variable in Java?

A) True as default value
B) False as default value
C) Null as default value
D) Undefined in Java memory

Correct Answer: B) False as default value
Explanation: In Java, boolean variables default to false when not explicitly initialized, ensuring predictable behavior.

4. Which operator is used to compare two values for equality in programming?

A) || operator for logic check
B) := operator for comparison
C) && operator for conditions
D) == operator for comparison

Correct Answer: D) == operator for comparison
Explanation: The == operator checks if two values are equal, returning True or False depending on the comparison.

5. Which of the following is an example of a ternary operator syntax?

A) condition ? value1 : value2
B) condition == value ? option
C) if(condition) {value1} else {value2}
D) option (condition) : value check

Correct Answer: A) condition ? value1 : value2
Explanation: The ternary operator is a shorthand for an if-else statement, evaluating a condition and returning one of two values.

6. What is the output of the following: print(type(5.0))?

A) <class ‘int’> in Python
B) <class ‘str’> in Python
C) <class ‘float’> in Python
D) Error in type check

Correct Answer: C) <class ‘float’> in Python
Explanation: The type() function in Python returns the type of the object. Since 5.0 is a floating-point number, the output is <class 'float'>.

7. Which keyword is used to define a function in Python?

A) func for defining functions
B) def for defining functions
C) lambda for defining functions
D) init for defining functions

Correct Answer: B) def for defining functions
Explanation: In Python, the def keyword is used to declare a function, followed by the function name and parameters in parentheses.

8. What is the output of the expression: "Hello" + "World" in most programming languages?

A) Hello World is displayed
B) HelloWorld is displayed
C) Error in concatenation syntax
D) Hello-World is displayed

Correct Answer: B) HelloWorld is displayed
Explanation: The + operator concatenates strings in most languages, combining "Hello" and "World" into "HelloWorld".

9. How do you create an infinite loop in Python?

A) while True: loop execution
B) for x in range(): loop execution
C) if x == 0: infinite check
D) do: condition until stop

Correct Answer: A) while True: loop execution
Explanation: A while True loop runs indefinitely because the condition is always true unless explicitly terminated with a break.

10. Which of the following is NOT an example of a loop?

A) for in iteration loop
B) while for conditional loop
C) if for checking condition
D) do-while for guaranteed loop

Correct Answer: C) if for checking condition
Explanation: An if statement is a conditional structure and not a loop, as it does not repeat execution based on conditions.

11. What is the main advantage of using a function in programming?

A) Avoid repetitive code blocks
B) Increase execution speed
C) Reduce memory consumption
D) Minimize number of variables

Correct Answer: A) Avoid repetitive code blocks
Explanation: Functions allow code reuse by enabling the same logic to be called multiple times, reducing redundancy and improving maintainability.

12. Which of the following is NOT a characteristic of a variable?

A) Variables store data values
B) Variables hold fixed data types
C) Variables always initialize memory
D) Variables allow data changes

Correct Answer: C) Variables always initialize memory
Explanation: Variables do not always initialize memory automatically; in some languages, they may hold garbage values if not explicitly initialized.

13. Which data structure is used to store a collection of key-value pairs?

A) Dictionary data structure
B) Array for storing elements
C) Stack for managing sequences
D) Queue for ordered elements

Correct Answer: A) Dictionary data structure
Explanation: Dictionaries store data as key-value pairs, allowing quick access to values based on their associated keys.

14. What is the output of the following code: if 0: print("True") else: print("False")?

A) True for non-zero check
B) False for zero condition
C) Error in logical syntax
D) True and False displayed

Correct Answer: B) False for zero condition
Explanation: In Python, 0 is considered False in a boolean context, so the else block is executed, printing "False".

15. How do you check if an element exists in a list in Python?

A) Use find(list, element) check
B) Use list.has(element) check
C) Use list.contains() method
D) Use element in list check

Correct Answer: D) Use element in list check
Explanation: The in keyword checks if an element exists in a list, returning True if the element is found.

16. What is the primary difference between while and do-while loops?

A) do-while checks at start; while checks end
B) while uses indices; do-while does not
C) do-while runs once; while may not
D) while needs initialization; do-while does not

Correct Answer: C) do-while runs once; while may not
Explanation: A do-while loop executes at least once before checking the condition, whereas a while loop checks the condition first.

17. Which of the following operators is used to access a member of an object?

A) The : operator in programming
B) The . operator in programming
C) The -> operator in functions
D) The @ operator in Python

Correct Answer: B) The . operator in programming
Explanation: The dot . operator is used to access attributes or methods of an object in most programming languages.

18. What is the result of the expression: 2 ** 3 in Python?

A) 6 from multiplication
B) 8 from exponentiation
C) 16 from squaring twice
D) Error due to syntax

Correct Answer: B) 8 from exponentiation
Explanation: The ** operator in Python performs exponentiation, so 2 ** 3 calculates 2^3=8.

19. Which function is used to convert a string to an integer in most programming languages?

A) int() for conversion
B) parseInt() method
C) convertInt() function
D) strToInt() function

Correct Answer: A) int() for conversion
Explanation: The int() function in Python and similar methods in other languages convert strings into integers.

20. Which of the following statements is correct about recursion?

A) A function calls itself
B) A function uses for loops
C) A function defines many variables
D) A function executes in reverse

Correct Answer: A) A function calls itself
Explanation: Recursion occurs when a function calls itself directly or indirectly, often used for solving complex problems with repeated subproblems.

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.