Mock Questions for the CBT Exam
Unit:
CIS096-1: Principles of Programming and Data Structure
CIS016-1: Principles of Programming
Variables:
1. What is a variable in Java?
- A) A type of Java method
- B) A container that holds values that can be changed
- C) A fixed value defined in Java
- D) A type of Java class
- Answer: B) A container that holds values that can be changed
2. Which of the following is a correct way to declare a variable in Java?
- A) int 1x;
- B) int x;
- C) float x;
- D) B and C are correct
- Answer: D) B and C are correct
3. What is the default value of an int variable in Java?
- A) 0
- B) null
- C) 1
- D) Not defined
- Answer: A) 0
4. Which keyword is used to declare a constant variable in Java?
- A) var
- B) constant
- C) static
- D) final
- Answer: D) final
5. What is the scope of a local variable in Java?
- A) Throughout the class
- B) Throughout the package
- C) Within the block it is declared
- D) Throughout the application
- Answer: C) Within the block it is declared
6. Which of these data types can store a decimal number in Java?
- A) int
- B) float
- C) boolean
- D) char
- Answer: B) float
7. What is the correct way to initialize a variable 'a' of type double to 9.8 in Java?
- A) double a = 9.8;
- B) double a == 9.8;
- C) a = 9.8d;
- D) double a = 9.8d;
- Answer: D) double a = 9.8d;
8. Which of the following variable names is invalid in Java?
- A) _myVar
- B) $myVar
- C) 2myVar
- D) myVar1
- Answer: C) 2myVar
9. In Java, which keyword is used for a variable that will not be serialized?
- A) static
- B) transient
- C) volatile
- D) final
- Answer: B) transient
10. What is type inference variable in Java, introduced in Java 10?
- A) var
- B) let
- C) const
- D) auto
- Answer: A) var
11. Which of these is not a primitive data type in Java?
- A) byte
- B) short
- C) String
- D) float
- Answer: C) String
12. What will happen if you try to assign a float value to a variable of type long without explicit
casting?
- A) Compilation error
- B) The float value will be rounded
- C) Runtime exception
- D) The value will be automatically casted
- Answer: A) Compilation error
13. Which of the following is true about static variables in Java?
- A) They can be accessed through objects only.
- B) They are initialized to null by default.
- C) They are shared among all instances of a class.
- D) They can be declared within methods.
- Answer: C) They are shared among all instances of a class.
14. What is the valid range of values for a Java byte variable?
- A) -128 to 127
- B) 0 to 255
- C) -256 to 255
- D) -32768 to 32767
- Answer: A) -128 to 127
15. Which of the following is a valid declaration of a char variable in Java?
- A) char myChar = 'A';
- B) char myChar = "A";
- C) char myChar = 65;
- D) A and C are correct
- Answer: D) A and C are correct
16. What is the result of declaring a variable inside a for-loop in Java?
- A) The variable can be used outside the for-loop.
- B) The variable is only
accessible within the for-loop.
- C) Compilation error.
- D) The variable is accessible throughout the class.
- Answer: B) The variable is only accessible within the for-loop.
17. Which keyword is used to define a variable that will have the same value for every instance of a
class?
- A) static
- B) final
- C) static final
- D) transient
- Answer: C) static final
18. How do you declare a variable that holds a large number (larger than 2 billion) in Java?
- A) int
- B) long
- C) double
- D) float
- Answer: B) long
19. In Java, what is the default value of a boolean variable?
- A) true
- B) false
- C) 0
- D) null
- Answer: B) false
20. Which of these is a valid declaration and initialization of an array in Java?
- A) int[] myArray = {1, 2, 3};
- B) int myArray[] = new int[3]{1, 2, 3};
- C) int myArray[] = new int[]{1, 2, 3};
- D) A and C are correct
- Answer: D) A and C are correct
Methods:
1. What is a function in Java?
- A) A block of code designed to perform a particular task
- B) A variable that stores data
- C) An operator used for arithmetic operations
- D) A data type
- Answer: A) A block of code designed to perform a particular task
2. How do you define a method in Java?
- A) datatype methodName()
- B) methodName datatype()
- C) datatype[] methodName()
- D) datatype methodName(parameters)
- Answer: D) datatype methodName(parameters)
3. Which keyword is used to define a method that does not return any value?
- A) null
- B) void
- C) empty
- D) none
- Answer: B) void
4. What is method overloading in Java?
- A) Changing the return type of a method
- B) Renaming a method
- C) Creating multiple methods with the same name but different parameters
- D) Deleting and recreating a method with different parameters
- Answer: C) Creating multiple methods with the same name but different parameters
5. Can a method in Java return multiple values?
- A) Yes, by using arrays or objects
- B) No, Java methods can only return a single value
- C) Yes, by using special keywords
- D) No, unless using external libraries
- Answer: A) Yes, by using arrays or objects
6. Which of the following is a valid method signature in Java?
- A) public double myMethod(int a, float b)
- B) private myMethod(double a, double b)
- C) static void myMethod(int a; int b)
- D) int myMethod(int a, int b)
- Answer: A) public double myMethod(int a, float b)
7. How do you call a static method named "calculate" that belongs to a class named
"MathOperations"?
- A) MathOperations.calculate();
- B) calculate.MathOperations();
- C) MathOperations->calculate();
- D) call MathOperations.calculate();
- Answer: A) MathOperations.calculate();
8. What is the purpose of the `this` keyword in a method?
- A) To refer to the current class instance
- B) To call another method within the class
- C) To declare static methods
- D) To return the current method
- Answer: A) To refer to the current class instance
9. Which of the following access modifiers allows a method to be accessed from any class in Java?
- A) private
- B) protected
- C) public
- D) default
- Answer: C) public
10. What does it mean if a method is "final" in Java?
- A) It cannot be overridden by subclasses
- B) It can be called without creating an instance of the class
- C) It does not return any value
- D) It can be overloaded within the same class
- Answer: A) It cannot be overridden by subclasses
11. What is a constructor in Java?
- A) A method that initializes an object
- B) A static method that runs when a class is loaded
- C) A final method that cannot be overridden
- D) A method that destroys an object
- Answer: A) A method that initializes an object
12. Which of the following is true about a method with a `void` return type?
- A) It cannot have parameters
- B) It must return null
- C) It does not return any value
- D) It returns an empty value
- Answer: C) It does not return any value
13. How do you indicate that a method parameter is optional in Java?
- A) By using the `Optional` keyword
- B) By initializing the parameter in the method definition
- C) Java does not support optional parameters directly
- D) By using overloading methods
- Answer: C) Java does not support optional parameters directly
14. What is recursion in Java?
- A) A method that calls another method
- B) A method that calls itself
- C) A loop that executes a block of code
- D) A static method that can be accessed without creating an instance
- Answer
: B) A method that calls itself
15. How can you achieve method overriding in Java?
- A) By creating methods with different names in the same class
- B) By creating static methods with the same signature in different classes
- C) By creating methods with the same signature in the subclass as in the parent class
- D) By changing the return type of an existing method
- Answer: C) By creating methods with the same signature in the subclass as in the parent class
16. What is the return type of a method that returns no value?
- A) int
- B) null
- C) void
- D) None of the above
- Answer: C) void
17. In method overloading, what must change?
- A) Method name
- B) Return type
- C) Number or type of parameters
- D) Access modifier
- Answer: C) Number or type of parameters
18. What does the `@Override` annotation indicate?
- A) The method cannot be overridden
- B) The method is static
- C) The method is overriding a method from a superclass
- D) The method is final
- Answer: C) The method is overriding a method from a superclass
19. Can constructor be overridden in Java?
- A) Yes
- B) No
- C) Only if it is public
- D) Only in subclasses
- Answer: B) No
20. Which of the following is an example of a variable argument (varargs) method in Java?
- A) public void myMethod(int... numbers)
- B) public void myMethod(int numbers[])
- C) public void myMethod(int numbers)
- D) public void myMethod(...int numbers)
- Answer: A) public void myMethod(int... numbers)
Conditional Statements
Certainly! Below are 15 multiple-choice questions (MCQs) focused on conditional statements in Java.
These questions will cover the use of `if`, `else`, `else if`, `switch`, and conditional operators,
providing a range of difficulty levels.
1. What is the basic form of a conditional statement in Java?
- A) `if (condition) { // statements }`
- B) `if condition then { // statements }`
- C) `condition ? if true : if false;`
- D) `if condition execute { // statements }`
- Answer: A) `if (condition) { // statements }`
2. How do you add an alternative condition to an `if` statement?
- A) `else (condition) { // statements }`
- B) `else if (condition) { // statements }`
- C) `elseif (condition) { // statements }`
- D) `if else (condition) { // statements }`
- Answer: B) `else if (condition) { // statements }`
3. Which operator is used for a simple conditional (ternary) operation?
- A) `? :`
- B) `if else`
- C) `->`
- D) `::`
- Answer: A) `? :`
4. What does the `switch` statement in Java support as of Java SE 7 and above?
- A) Integer values only
- B) String values
- C) Floating-point values
- D) Objects of any class
- Answer: B) String values
5. What is the output of the following code snippet if `x = 5`?
if (x > 3)
System.out.println("Apple");
else
System.out.println("Banana");
System.out.println("Cherry");
- A) Apple
- B) Banana
- C) Apple Cherry
- D) Banana Cherry
- Answer: C) Apple Cherry
6. Which of the following is NOT a valid way to use an `if` statement in Java?
- A) `if (1 < 2) { // statements }`
- B) `if (1 < 2; 2 < 3) { // statements }`
- C) `if (1 < 2) System.out.println("True");`
- D) `if (1 < 2) { } else { }`
- Answer: B) `if (1 < 2; 2 < 3) { // statements }`
7. How do you execute multiple cases in a `switch` statement that share the same block of code?
- A) By separating each case with a comma
- B) By using the `fallthrough` keyword
- C) By not including a `break` statement in each case
- D) By using the `continue` keyword
- Answer: C) By not including a `break` statement in each case
8. What will be the result of executing the following code if `char grade = 'C';`?
switch(grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
case 'C':
System.out.println("Well done");
break;
default:
System.out.println("Invalid grade");
- A) Excellent
- B) Well done
- C) Invalid grade
- D) No output
- Answer: B) Well done
9. In a ternary operator `condition ? expr1 : expr2;`, what happens if `condition` is true?
- A) `expr1` is executed
- B) `expr2` is executed
- C) Both `expr1` and `expr2` are executed
- D) Neither `expr1` nor `expr2` is executed
- Answer: A) `expr1` is executed
10. Which of the following is a correct `if-else` statement in Java?
- A) `if (x > y) System.out.println("X"); else System.out.println("Y");`
- B) `if x > y: System.out.println("X"); else: System.out.println("Y");`
- C) `if (x > y) then System.out.println("X") else System.out.println("Y");`
- D) `if x > y System.out.println("X") else System.out.println("Y");`
- Answer: A) `if (x > y) System.out.println("X"); else System.out.println("Y");`
11. What is required for an `else if` ladder to work correctly?
- A) Each `else if` condition must be mutually exclusive
- B) The `else` block at the end is mandatory
- C) All conditions must be true
- D) The `if` statement can only test for equality
- Answer: A) Each `else if` condition must be mutually exclusive
12. What is the purpose of the `default` case in a `switch` statement?
- A) To define the default value of the switch variable
- B) To catch any case not explicitly handled by the case statements
- C) To set the default behavior if the switch condition is true
- D) To terminate the switch statement
- Answer: B) To catch any case not explicitly handled by the case statements
13. How can you refactor an `if-else` chain that checks the same variable for equality against
multiple values?
- A) Using a `while` loop
- B) Using a `for` loop
- C) Using a `switch` statement
- D) Using ternary operators
- Answer: C) Using a `switch` statement
14. What will happen if a `break` statement is omitted from a case in a `switch` statement?
- A) The program will terminate immediately
- B) The `switch` statement will fail to compile
- C) Subsequent cases will be executed until a `break` is encountered
- D) Only the next case will be executed
- Answer: C) Subsequent cases will be executed until a `break` is encountered
15. What is the correct syntax for using multiple conditions in an `if` statement?
- A) `if (condition1 && condition2) { // statements }`
- B) `if (condition1) && (condition2) { // statements }`
- C) `if condition1 and condition2 { // statements }`
- D) `if condition1 & condition2 { // statements }`
- Answer: A) `if (condition1 && condition2) { // statements }`
Loops:
1. What is the purpose of a loop in Java?
- A) To store multiple values in a single variable
- B) To execute a block of statements repeatedly
- C) To define a class or method
- D) To catch exceptions
- Answer: B) To execute a block of statements repeatedly
2. Which statement is true about the `for` loop in Java?
- A) It runs at least once before evaluating its condition
- B) It is used to iterate a block of code a known number of times
- C) It is used only with array data structures
- D) It cannot be used for iterating over collections
- Answer: B) It is used to iterate a block of code a known number of times
3. What is the correct syntax for a `while` loop in Java?
- A) `while (condition) { // statements }`
- B) `while condition: // statements`
- C) `while { // statements } (condition)`
- D) `while (condition; // statements;)`
- Answer: A) `while (condition) { // statements }`
4. How do you ensure a `do-while` loop terminates?
- A) By initializing the loop variable before the loop starts
- B) By using a `break` statement within the loop
- C) By ensuring the loop condition eventually evaluates to false
- D) By omitting the loop condition
- Answer: C) By ensuring the loop condition eventually evaluates to false
5. Which loop would you use if you do not know how many times you need to iterate in advance?
- A) `for` loop
- B) `while` loop
- C) `do-while` loop
- D) Infinite loop
- Answer: B) `while` loop
6. What does the `break` statement do in a loop?
- A) Pauses the loop
- B) Skips the current iteration
- C) Terminates the loop immediately
- D) None of the above
- Answer: C) Terminates the loop immediately
7. How can you skip to the next iteration of a loop in Java?
- A) Using the `skip` statement
- B) Using the `break` statement
- C) Using the `continue` statement
- D) By incrementing the loop counter
- Answer: C) Using the `continue` statement
8. Which of the following is a valid `for` loop declaration in Java?
- A) `for int i=0; i<10; i++ { }`
- B) `for (i=0; i<10; i++) { }`
- C) `for (int i=0; i<10; i++) { }`
- D) `for i=0 to 9 { }`
- Answer: C) `for (int i=0; i<10; i++) { }`
9. In a `do-while` loop, when is the condition checked?
- A) Before the loop starts
- B) Immediately after the first iteration
- C) At the end of each iteration
- D) At the beginning of each iteration
- Answer: C) At the end of each iteration
10. Which of the following is NOT a loop control statement in Java?
- A) `continue`
- B) `exit`
- C) `break`
- D) `return`
- Answer: B) `exit`
11. What is an infinite loop?
- A) A loop that executes a finite number of times
- B) A loop that cannot execute
- C) A loop that executes indefinitely
- D) A loop that executes once
- Answer: C) A loop that executes indefinitely
12. How do you write a `for` loop that starts at 0 and increments by 2 each time up to 10?
- A) `for (int i = 0; i <= 10; i += 2)`
- B) `for (int i = 0; i < 10; i++)`
- C) `for (int i = 1; i <= 10; i = 2)`
- D) `for (int i = 0; i <= 10; i--)`
- Answer: A) `for (int i = 0; i <= 10; i += 2)`
13. Which loop is preferred when iterating through an array or a collection?
- A) `while` loop
- B) `do-while` loop
- C) Enhanced `for` loop
- D) Traditional `for` loop
- Answer: C) Enhanced `for` loop
14. What is the output of the following code snippet?
int i = 0;
while(i < 5) {
i++;
if(i == 3) continue;
System.out.print(i + " ");
- A) 1 2 4 5
- B) 1 2 3 4 5
- C) 1 2 3 4
- D) 2 3 4 5
- Answer: D) 2 3 4 5
15. What is the role of the initialization statement in a `for` loop?
- A) It specifies how many times the loop will execute.
- B) It initializes the loop control variable.
- C) It is executed at the end of each iteration.
- D) It determines the loop's exit condition.
- Answer: B) It initializes the loop control variable.
16. Which of the following would you use to loop through the elements of an array named `arr`?
- A) `for (int i = 0; i < arr.length(); i++) { }`
- B) `for (int i : arr) { }`
- C) Both A and B
- D) None of the above
- Answer: C) Both A and B
17. What is the effect of a `continue` statement in a `for` loop?
- A) It causes the loop to skip the remaining code and proceed with the next iteration.
- B) It exits the loop immediately.
- C) It repeats the current iteration.
- D) It does nothing.
- Answer: A) It causes the loop to skip the remaining code and proceed with the next iteration.
18. What is the output of the following `for` loop?
for (int i = 5; i > 0; i--) {
System.out.print(i + " ");
- A) 5 4 3 2 1
- B) 1 2 3 4 5
- C) 5 4 3 2
- D) 4 3 2 1 0
- Answer: A) 5 4 3 2 1
19. In which scenario would you prefer a `do-while` loop over a `while` loop?
- A) When you want to check the condition at the start of the loop
- B) When you want the loop to execute at least once regardless of the condition
- C) When you are not sure if the loop should execute
- D) When you need to iterate through a collection
- Answer: B) When you want the loop to execute at least once regardless of the condition
20. What will happen if you forget to update the loop variable in a `while` loop?
- A) The loop will execute a fixed number of times.
- B) The loop will terminate immediately.
- C) The loop may become an infinite loop.
- D) The loop will skip every other iteration.
- Answer: C) The loop may become an infinite loop.
Class and objects:
1. What is a class in Java?
- A) A template or blueprint from which objects are created
- B) A specific instance of an object
- C) A data type that stores a fixed value
- D) A method that performs an action
- Answer: A) A template or blueprint from which objects are created
2. What is an object in Java?
- A) A type of variable
- B) A template for creating classes
- C) An instance of a class
- D) A method within a class
- Answer: C) An instance of a class
3. How do you create an object in Java?
- A) `ClassName objectName;`
- B) `new ClassName();`
- C) `ClassName objectName = new ClassName();`
- D) `create ClassName objectName;`
- Answer: C) `ClassName objectName = new ClassName();`
4. Which keyword is used to inherit a class in Java?
- A) `extends`
- B) `implements`
- C) `super`
- D) `this`
- Answer: A) `extends`
5. What is encapsulation in Java?
- A) The process of wrapping code and data together into a single unit
- B) The ability of an object to take on many forms
- C) The mechanism of basing a new class on an existing class
- D) Breaking down a complex problem into smaller chunks
- Answer: A) The process of wrapping code and data together into a single unit
6. How do you call a method named `methodName` of an object named `objectName`?
- A) `methodName(objectName);`
- B) `ClassName.methodName();`
- C) `objectName.methodName();`
- D) `methodName.call(objectName);`
- Answer: C) `objectName.methodName();`
7. Which keyword is used to refer to the current instance of a class?
- A) `self`
- B) `this`
- C) `super`
- D) `current`
- Answer: B) `this`
8. How do you define a constructor in Java?
- A) A method with the same name as the class and no return type
- B) A static method that initializes the class
- C) A method named `construct`
- D) A method with the name `constructor()`
- Answer: A) A method with the same name as the class and no return type
9. What is polymorphism in Java?
- A) The ability of a class to hide its internal details
- B) The capability of a method to do different things based on the object it is acting upon
- C) The concept of restricting access to some of the object's components
- D) The practice of using class inheritance to derive new classes
- Answer: B) The capability of a method to do different things based on the object it is acting upon
10. What does it mean if a class is abstract?
- A) It cannot be instantiated, but it can be subclassed
- B) It can be instantiated, but cannot be subclassed
- C) It can only contain static methods
- D) It hides all its methods from other classes
- Answer: A) It cannot be instantiated, but it can be subclassed
11. Which method is called when an object is created?
- A) `init()`
- B) `start()`
- C) `main()`
- D) Constructor
- Answer: D) Constructor
12. How do you make a class variable (field) accessible only within its own class?
- A) Declare it using the `public` modifier
- B) Declare it using the `private` modifier
- C) Declare it using the `protected` modifier
- D) Declare it without any access modifier
- Answer: B) Declare it using the `private` modifier
13. What is the output of calling `toString()` method on an object if it is not overridden?
- A) The object's hash code
- B) The name of the class followed by '@' and the object's hashcode
- C) The string representation of the object's data
- D) An error message saying the method is not implemented
- Answer:
B) The name of the class followed by '@' and the object's hashcode
14. Which principle of OOP does Java not support directly?
- A) Encapsulation
- B) Inheritance
- C) Polymorphism
- D) Multiple inheritance of classes
- Answer: D) Multiple inheritance of classes
15. What is method overloading?
- A) Changing the return type of a method
- B) Creating a method with the same name but different parameters in the same class
- C) Creating a method in a subclass with the same signature as one in its superclass
- D) Renaming a method in a subclass
- Answer: B) Creating a method with the same name but different parameters in the same class
Extra Class and objects:
1. Is the following class definition syntax correct in Java?
public class MyClass {
int myField;
public void MyClass() {
myField = 5;
- A) Correct
- B) Incorrect
- Answer: A) Correct (Note: While this looks like a constructor, it's actually a method because of
the `void` return type. It's a common mistake but syntactically correct.)
2. Is this method of creating a new object instance correct?
MyClass obj = new MyClass();
- A) Correct
- B) Incorrect
- Answer: A) Correct
3. Evaluate the syntax for declaring a class and creating an object:
class MyClass {}
MyClass obj = MyClass.new();
- A) Correct
- B) Incorrect
- Answer: B) Incorrect (The correct syntax should be `MyClass obj = new MyClass();`)
4. Is this constructor definition correct in Java?
public MyClass() {
System.out.println("Constructor called.");
- A) Correct
- B) Incorrect
- Answer: A) Correct
5. Check the correctness of the following inheritance syntax:
public class MySubclass extends MyClass {
// Implementation
}
- A) Correct
- B) Incorrect
- Answer: A) Correct
6. Is the following method overloading valid in Java?
public class MyClass {
void myMethod(int a) { }
int myMethod(int b) { return b; }
- A) Correct
- B) Incorrect
- Answer: B) Incorrect (Method overloading is not valid here because the methods have the
same parameter list. Return type does not affect overloading.)
7. Assess the validity of this abstract class definition:
abstract class MyAbstractClass {
abstract void myMethod();
- A) Correct
- B) Incorrect
- Answer: A) Correct
8. Is this way of defining a static method in a class correct?
public class MyClass {
public static void myStaticMethod() {
System.out.println("Static method called.");
- A) Correct
- B) Incorrect
- Answer: A) Correct
9. Evaluate the correctness of implementing multiple interfaces:
public class MyClass implements InterfaceOne, InterfaceTwo {
// Method implementations
- A) Correct
- B) Incorrect
- Answer: A) Correct
10. Is the following use of `this` keyword correct in Java?
public class MyClass {
int x;
public MyClass(int x) {
this.x = x;
- A) Correct
- B) Incorrect
- Answer: A) Correct
11. Check the syntax for declaring an interface:
interface MyInterface {
public void myMethod();
- A) Correct
- B) Incorrect
- Answer: A) Correct
12. Is this way of accessing a static variable correct?
public class MyClass {
static int myStaticVar = 10;
int value = MyClass.myStaticVar;
- A) Correct
- B) Incorrect
- Answer: B) Incorrect (It's syntactically correct, but it should be inside a method or a block to be
contextually correct.)
13. Evaluate this approach to method overriding:
public class MySubclass extends MyClass {
@Override
public void myMethod() {
// Implementation
- A) Correct
- B) Incorrect
- Answer: A) Correct
14. Is the following private method inheritance correct?
public class MyClass {
private void myMethod() { }
public class MySubclass extends MyClass {
public void myMethod() { }
- A) Correct
- B) Incorrect
- Answer: A) Correct (While private methods cannot be overridden, this is syntactically correct
as it's considered a new method in the subclass.)
15. Assess the correctness of this encapsulation example:
public class
MyClass {
private int myVar;
public int getMyVar() {
return myVar;
public void setMyVar(int myVar) {
this.myVar = myVar;
- A) Correct
- B) Incorrect
- Answer: A) Correct
Arrays, ArrayLists and linkedList:
### Array
1. What is the correct way to declare an array in Java?
- A) `int array[10];`
- B) `int[] array;`
- C) `array[] int;`
- D) `int array();`
- Answer: B) `int[] array;`
2. How do you initialize an array in Java?
- A) `int[] array = new int[5];`
- B) `int array = new int[5];`
- C) `int[] array = int[5];`
- D) `int array[] = new int[];`
- Answer: A) `int[] array = new int[5];`
3. What is the default value of an int array in Java?
- A) 0
- B) null
- C) 1
- D) Not set
- Answer: A) 0
4. How do you access the third element of an array named `arr`?
- A) `arr[2];`
- B) `arr[3];`
- C) `arr.get(2);`
- D) `get(arr[2]);`
- Answer: A) `arr[2];`
5. What does the `length` attribute of an array provide?
- A) The memory size of the array
- B) The number of elements it can hold
- C) The number of elements in the array
- D) The maximum value in the array
- Answer: C) The number of elements in the array
### ArrayLists
6. How do you instantiate an ArrayList in Java?
- A) `ArrayList al = new ArrayList();`
- B) `ArrayList[] al = new ArrayList();`
- C) `ArrayList al = ArrayList();`
- D) `ArrayList al = new ArrayList[];`
- Answer: A) `ArrayList al = new ArrayList();`
7. Which of these is a method to add an element to an ArrayList named `al`?
- A) `al.add(element);`
- B) `al.append(element);`
- C) `al.insert(element);`
- D) `add(al, element);`
- Answer: A) `al.add(element);`
8. How do you remove an element from an ArrayList at index 2?
- A) `al.remove(2);`
- B) `al.delete(2);`
- C) `remove(al[2]);`
- D) `al.removeElement(2);`
- Answer: A) `al.remove(2);`
9. What does the `size()` method of an ArrayList return?
- A) The capacity of the ArrayList
- B) The current number of elements in the ArrayList
- C) The maximum size the ArrayList can grow to
- D) The memory size of the ArrayList
- Answer: B) The current number of elements in the ArrayList
10. How do you ensure type safety in an ArrayList?
- A) Use a `SafeArrayList`
- B) Specify the type using generics, e.g., `ArrayList<String>`
- C) Use the `safe` keyword, e.g., `safe ArrayList`
- D) Type safety cannot be ensured in ArrayLists
- Answer: B) Specify the type using generics, e.g., `ArrayList<String>`
### LinkedLists
11. Which of the following is true about a LinkedList in Java?
- A) It can only be used with primitive data types
- B) It allows for constant time insertions or removals using iterators
- C) It does not allow duplicate elements
- D) Access time for an element is constant
- Answer: B) It allows for constant time insertions or removals using iterators
12. How do you add an element to the front of a LinkedList named `ll`?
- A) `ll.addFirst(element);`
- B) `ll.push(element);`
- C) `ll.add(0, element);`
- D) All of the above
- Answer: D) All of the above
13. What method do you use to retrieve but not remove the head of a LinkedList?
- A) `getFirst()`
- B) `peek()`
- C) `element()`
- D) `B` and `C` are correct
- Answer:
D) `B` and `C` are correct
14. How do you remove the first occurrence of a specified element in a LinkedList?
- A) `removeFirstOccurrence(Object o)`
- B) `delete(Object o)`
- C) `remove(Object o)`
- D) `unlink(Object o)`
- Answer: A) `removeFirstOccurrence(Object o)`
15. Which of these data structures would you use to efficiently insert or remove elements
from the middle of a collection?
- A) Array
- B) ArrayList
- C) LinkedList
- D) All of the above
- Answer: C) LinkedList