Example Questions For CBT Exam
Example Questions For 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) int 1x;
- B) int x;
- C) float x;
- A) 0
- B) null
- C) 1
- D) Not defined
- Answer: A) 0
- A) var
- B) constant
- C) static
- D) final
- Answer: D) final
- 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;
- 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
- A) var
- B) let
- C) const
- D) auto
- Answer: A) var
- 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
- C) Runtime exception
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
- C) Compilation error.
- A) static
- B) final
- C) static final
- D) transient
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
- A) true
- B) false
- C) 0
- D) null
- Answer: B) false
- D) A data type
- A) datatype methodName()
- B) methodName datatype()
- C) datatype[] methodName()
- 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
- B) Renaming a method
- C) Creating multiple methods with the same name but different parameters
- Answer: C) Creating multiple methods with the same name but different parameters
5. Can a method in Java return multiple values?
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();
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
12. Which of the following is true about a method with a `void` return type?
- Answer
- C) By creating methods with the same signature in the subclass as in the parent class
- Answer: C) By creating methods with the same signature in the subclass as in the parent class
- A) int
- B) null
- C) void
- Answer: C) void
- A) Method name
- B) Return type
- D) Access modifier
- 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?
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.
- A) `? :`
- B) `if else`
- C) `->`
- D) `::`
- Answer: A) `? :`
4. What does the `switch` statement in Java support as of Java SE 7 and above?
- B) String values
- C) Floating-point values
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
6. Which of the following is NOT a valid way to use an `if` statement in Java?
7. How do you execute multiple cases in a `switch` statement that share the same block of code?
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
- A) `expr1` is executed
- B) `expr2` is executed
- 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?
14. What will happen if a `break` statement is omitted from a case in a `switch` statement?
15. What is the correct syntax for using multiple conditions in an `if` statement?
Loops:
1. What is the purpose of a loop in Java?
- D) To catch exceptions
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
- D) `for i=0 to 9 { }`
- A) `continue`
- B) `exit`
- C) `break`
- D) `return`
- Answer: B) `exit`
12. How do you write a `for` loop that starts at 0 and increments by 2 each time up to 10?
- A) `while` loop
- B) `do-while` loop
int i = 0;
while(i < 5) {
i++;
if(i == 3) continue;
- 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
16. Which of the following would you use to loop through the elements of an array named `arr`?
- C) Both A and B
- A) It causes the loop to skip the remaining code and proceed with the next iteration.
- B) It exits the loop immediately.
- D) It does nothing.
- Answer: A) It causes the loop to skip the remaining code and proceed with the next iteration.
- 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
- 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) A type of variable
- C) An instance of a class
- A) `ClassName objectName;`
- B) `new ClassName();`
- A) `extends`
- B) `implements`
- C) `super`
- D) `this`
- Answer: A) `extends`
- Answer: A) The process of wrapping code and data together into a single unit
- A) `methodName(objectName);`
- B) `ClassName.methodName();`
- C) `objectName.methodName();`
- D) `methodName.call(objectName);`
- Answer: C) `objectName.methodName();`
- A) `self`
- B) `this`
- C) `super`
- D) `current`
- Answer: B) `this`
- A) A method with the same name as the class and no return type
- Answer: A) A method with the same name as the class and no return type
- B) The capability of a method to do different things based on the object it is acting upon
- Answer: B) The capability of a method to do different things based on the object it is acting upon
- 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?
13. What is the output of calling `toString()` method on an object if it is not overridden?
- B) The name of the class followed by '@' and the object's hashcode
- Answer:
B) The name of the class followed by '@' and the object's hashcode
- A) Encapsulation
- B) Inheritance
- C) Polymorphism
- 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
- Answer: B) Creating a method with the same name but different parameters in the same class
int myField;
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.)
- A) Correct
- B) Incorrect
- Answer: A) Correct
class MyClass {}
- A) Correct
- B) Incorrect
- Answer: B) Incorrect (The correct syntax should be `MyClass obj = new MyClass();`)
public MyClass() {
System.out.println("Constructor called.");
- A) Correct
- B) Incorrect
- Answer: A) Correct
// Implementation
}
- A) Correct
- B) Incorrect
- Answer: A) Correct
void myMethod(int a) { }
- 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.)
- A) Correct
- B) Incorrect
- Answer: A) Correct
- A) Correct
- B) Incorrect
- Answer: A) Correct
// Method implementations
- A) Correct
- B) Incorrect
- Answer: A) Correct
int x;
public MyClass(int x) {
this.x = x;
- A) Correct
- B) Incorrect
- Answer: A) Correct
interface MyInterface {
- A) Correct
- B) Incorrect
- Answer: A) Correct
12. Is this way of accessing a static variable correct?
- 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.)
@Override
// Implementation
- A) Correct
- B) Incorrect
- Answer: A) Correct
- 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.)
public class
MyClass {
return 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;`
### ArrayLists
### LinkedLists
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:
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