MCQs on Programming Basics

[Set - 4]

1. Which of the following is an immutable data type in Python?

A) List is mutable and flexible
B) String is immutable in Python
C) Set is mutable in operations
D) Dictionary is changeable type

Correct Answer: B) String is immutable in Python
Explanation: Strings in Python cannot be modified after creation. Any operation on a string creates a new string instead of altering the original.

2. How can you concatenate two lists in Python?

A) Use the + operator for lists
B) Use the append method in loops
C) Use the range function directly
D) Use the iter function on lists

Correct Answer: A) Use the + operator for lists
Explanation: The + operator combines two lists by creating a new list containing all elements from both lists.

3. Which of the following is NOT an arithmetic operator?

A) + for addition operation
B) // for integer division
C) and for logical comparison
D) % for modulus operation

Correct Answer: C) and for logical comparison
Explanation: The and keyword is a logical operator used for conditional statements, not for arithmetic operations.

4. What is the result of bool([]) in Python?

A) True because it’s valid
B) False because it’s empty
C) Error due to syntax
D) Null for invalid input

Correct Answer: B) False because it’s empty
Explanation: An empty list is considered False in a boolean context in Python. Non-empty lists return True.

5. What is the default value of a static variable in Java?

A) Null for all static types
B) False for boolean values
C) Zero for all data types
D) Garbage for uninitialized

Correct Answer: B) False for boolean values
Explanation: In Java, static boolean variables are initialized to false by default, while numeric types default to 0.

6. How do you append an element to a list in Python?

A) Use the pop() function
B) Use the insert() function
C) Use the len() function
D) Use the append() function

Correct Answer: D) Use the append() function
Explanation: The append() method adds an element to the end of the list without modifying existing elements.

7. Which function is used to remove an element from a list in Python?

A) pop() removes specific index
B) append() adds elements
C) clear() empties the list
D) count() finds occurrences

Correct Answer: A) pop() removes specific index
Explanation: The pop() method removes and returns the element at a specific index, with the default being the last element.

8. What is the output of type(None) in Python?

A) <class ‘int’> for numerical
B) <class ‘NoneType’> output
C) <class ‘str’> for strings
D) <class ‘object’> general

Correct Answer: B) <class ‘NoneType’> output
Explanation: The type() function returns <class 'NoneType'> when passed None, which is a unique data type in Python.

9. Which keyword is used to create an infinite loop in Java?

A) Use while(true) syntax
B) Use do-while(true) loop
C) Use for(;;) with no conditions
D) Use loop(true) for execution

Correct Answer: C) Use for(;;) with no conditions
Explanation: The for(;;) syntax in Java creates an infinite loop by omitting all loop control parameters.

10. What is the difference between a tuple and a list in Python?

A) Tuples are immutable data types
B) Lists can only hold strings
C) Tuples use square brackets
D) Lists cannot hold duplicates

Correct Answer: A) Tuples are immutable data types
Explanation: Tuples cannot be modified after creation, while lists are mutable and allow changes.

11. How do you write a nested function in Python?

A) lambda function() inner()
B) function(outer): call inner
C) def outer(): def inner()
D) init function(): nested defs

Correct Answer: C) def outer(): def inner()
Explanation: In Python, nested functions are written by defining one function inside another, enabling encapsulated logic.

12. What will the following code output? print('5' + '5')

A) 10 as an integer result
B) 55 as a concatenated string
C) Error in type coercion
D) Undefined due to data types

Correct Answer: B) 55 as a concatenated string
Explanation: The + operator concatenates strings. Since both '5' and '5' are strings, the result is '55'.

13. What is the correct syntax to initialize a variable in JavaScript?

A) var x = 5; declaration
B) define x = 5; assignment
C) initialize x with let 5
D) type x: number assign 5

Correct Answer: A) var x = 5; declaration
Explanation: In JavaScript, the var or let keyword is used to declare variables, followed by the assignment operator =.

14. How can you create a set in Python?

A) list() to avoid duplicates
B) {} to store unique items
C) set() to initialize values
D) dict() to define values

Correct Answer: C) set() to initialize values
Explanation: The set() function creates a collection of unique, unordered elements. {} is also used but can be ambiguous with empty dictionaries.

15. What is the difference between mutable and immutable data types?

A) Mutable only stores numbers
B) Immutable changes contents
C) Mutable allows data change
D) Immutable defines a sequence

Correct Answer: C) Mutable allows data change
Explanation: Mutable data types, like lists, can be modified after creation, while immutable types, like tuples, cannot.

16. What is the output of list(range(3)) in Python?

A) [0, 1, 2] as a sequence
B) [1, 2, 3] by increment
C) Error in argument type
D) Null for undefined range

Correct Answer: A) [0, 1, 2] as a sequence
Explanation: The range(3) function generates numbers from 0 to 2, and list() converts them into a list.

17. Which keyword is used to inherit a class in Python?

A) class extends parent()
B) class derived from()
C) class child(base): define
D) class child(parent): base

Correct Answer: D) class child(parent): base
Explanation: In Python, inheritance is specified by placing the parent class name inside parentheses after the child class name.

18. Which of the following is a characteristic of object-oriented programming?

A) Sequential data in arrays
B) Functions with no state
C) Conditional loops in logic
D) Encapsulation in methods

Correct Answer: D) Encapsulation in methods
Explanation: Object-oriented programming emphasizes encapsulation, where data and functions are bundled into objects.

19. What is the purpose of the __init__ method in Python?

A) Initialize object attributes
B) Define a method as static
C) Terminate object execution
D) Encapsulate functions safely

Correct Answer: A) Initialize object attributes
Explanation: The __init__ method is a constructor in Python, called when an object is created to initialize its attributes.

20. How do you access a superclass method in Python?

A) Use super().method()
B) Use parent.method()
C) Use child.class() method
D) Use extends.super() logic

Correct Answer: A) Use super().method()
Explanation: The super() function allows access to methods from a superclass, enabling method overriding or augmentation.

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.