Unit 2 - Practice Quiz

CSE310

1 Which of the following creates an infinite loop in Java?

A. for(;;)
B. while(true)
C. do { } while(true);
D. All of the above

2 What is the output of the following code snippet?

java
int x = 0;
do {
x++;
} while (false);
System.out.println(x);

A.
B. 1
C. Compilation Error
D. Infinite Loop

3 Which statement regarding the enhanced for-each loop is true?

A. It gives access to the index of the element.
B. It can be used to modify the structure of a collection (e.g., removing elements) while iterating.
C. It iterates through elements sequentially without using an explicit counter.
D. It runs faster than a standard for-loop in all cases.

4 What is the correct syntax to declare a two-dimensional integer array with 3 rows and 4 columns?

A. int array = new int[3,4];
B. int[][] array = new int[3][4];
C. int array[][] = new int[4][3];
D. int[3][4] array = new int;

5 Consider the array declaration: int[] arr = new int[5];. What is the default value stored at index 0?

A. null
B.
C. undefined
D. -1

6 Which property is used to determine the size of an array named numbers?

A. numbers.length()
B. numbers.size()
C. numbers.length
D. numbers.count

7 What happens if you try to access arr[5] in an array defined as int[] arr = new int[5];?

A. It returns 0.
B. It returns null.
C. It throws an ArrayIndexOutOfBoundsException.
D. It causes a compilation error.

8 Which of the following statements about Varargs is correct?

A. There can be multiple varargs parameters in a method.
B. The varargs parameter must be the first parameter in the method signature.
C. The varargs parameter must be the last parameter in the method signature.
D. Varargs cannot accept an array as an argument.

9 How is the method public void calculate(int... numbers) treated internally by Java?

A. As a method taking a List<Integer>.
B. As a method taking an array int[] numbers.
C. As a series of overloaded methods.
D. As a method taking a single int.

10 Given the following Enum:
java
enum Day { MONDAY, TUESDAY }

What does Day.MONDAY.ordinal() return?

A. "MONDAY"
B. 1
C.
D. Day

11 Which method is automatically added to every Enum by the compiler to return an array of all constants?

A. getAll()
B. values()
C. list()
D. constants()

12 Can an Enum in Java have a constructor?

A. No, Enums cannot have constructors.
B. Yes, but it must be public.
C. Yes, but it must be private or package-private.
D. Yes, but it must be protected.

13 What is the main purpose of a Constructor in a Java class?

A. To return an object of the class.
B. To initialize the newly created object.
C. To allocate memory for the object.
D. To destroy the object.

14 If a class does not define any constructor, what happens?

A. The compiler generates a default no-argument constructor.
B. The code will not compile.
C. The class cannot be instantiated.
D. The JVM throws a runtime error.

15 Identify the invalid method declaration:

A. void method() { }
B. int method(int a) { return a; }
C. method() { }
D. static void method() { }

16 Which of the following best describes Method Overloading?

A. Methods with the same name and same parameters in different classes.
B. Methods with the same name but different return types only.
C. Methods with the same name but different parameter lists within the same class.
D. Overriding a method from a parent class.

17 Can you overload a method based solely on its return type?

A. Yes, always.
B. No, never.
C. Yes, if the return type is a subclass.
D. Yes, if the method is static.

18 What is the purpose of the this keyword?

A. To refer to the parent class.
B. To refer to the static members of the class.
C. To refer to the current object instance.
D. To create a new thread.

19 In a constructor, how do you call another constructor of the same class?

A. super()
B. call()
C. this()
D. constructor()

20 Where must the call to this() be placed within a constructor?

A. Anywhere in the constructor.
B. It must be the first statement.
C. It must be the last statement.
D. Only inside a try-catch block.

21 When does an Instance Initializer Block run?

A. When the class is loaded.
B. Before the constructor is invoked, every time an object is created.
C. After the constructor finishes.
D. Only when explicitly called.

22 When does a Static Initializer Block run?

A. Every time an object is created.
B. Only once, when the class is loaded by the ClassLoader.
C. When the main method ends.
D. When a static method is called, every time.

23 Which of the following is true about String objects in Java?

A. They are mutable.
B. They are immutable.
C. They act like primitive data types.
D. They use the stack memory only.

24 What is the output of the following code?
java
String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);

A. true
B. false
C. Compilation Error
D. NullPointerException

25 Which method is used to compare the content of two String objects?

A. ==
B. compareTo()
C. equals()
D. contentEquals()

26 Which class should be used for frequent String modifications to ensure efficiency?

A. String
B. StringBuilder
C. StringBuffer (if thread safety is not required)
D. CharBuffer

27 What is the default capacity of a StringBuilder when initialized with no arguments?

A.
B. 10
C. 16
D. 32

28 How do you define a Ragged (or Jagged) Array in Java?

A. int[][] arr = new int[3][];
B. int[][] arr = new int[][3];
C. int arr[][] = {1, 2, 3};
D. Ragged arrays are not supported in Java.

29 What output does System.out.println(10 + 20 + "Java"); produce?

A. 1020Java
B. 30Java
C. Java1020
D. Java30

30 Which operator is used to check if an object is an instance of a specific class?

A. typeof
B. instanceof
C. isA
D. as

31 Consider int x = 5;. What is the result of ++x vs x++?

A. No difference.
B. ++x increments then uses the value; x++ uses the value then increments.
C. x++ increments then uses the value; ++x uses the value then increments.
D. Both cause compilation errors.

32 What is the correct way to get the character at index 2 of a string s?

A. s[2]
B. s.get(2)
C. s.charAt(2)
D. s.char(2)

33 What is the scope of a variable declared inside a for loop initialization block (e.g., for(int i=0;...)?

A. The entire class.
B. The entire method.
C. Only within the for loop block.
D. From the declaration point to the end of the file.

34 Which keyword is used to stop the execution of the current loop immediately?

A. stop
B. return
C. break
D. continue

35 Given int[] a = {1, 2, 3}; int[] b = a;, if you change b[0] = 5, what is the value of a[0]?

A. 1
B. 5
C.
D. Undefined

36 Which method signature allows the main method to be the entry point of a Java application?

A. public void main(String[] args)
B. public static void main(String args)
C. public static void main(String[] args)
D. static void main(String... args)

37 In the context of OOP, what is an Object?

A. A blueprint for a class.
B. A runtime instance of a class.
C. A primitive data type.
D. A static method.

38 Can this be used in a static context (e.g., a static method)?

A. Yes.
B. No.
C. Only if the class is abstract.
D. Only to call static variables.

39 What is the result of String s = " Hello "; s.trim(); if we print s afterward?

A. "Hello"
B. " Hello "
C. "Hello "
D. null

40 Which of the following is a valid constructor for a class named Car?

A. public void Car() {}
B. public car() {}
C. Car() {}
D. new Car() {}

41 In a 2D array, int[][] table, what does table.length represent?

A. The total number of integers in the array.
B. The number of columns.
C. The number of rows.
D. The size in bytes.

42 How do you access the element in the 2nd row and 3rd column of array matrix?

A. matrix[2][3]
B. matrix[1][2]
C. matrix[3][2]
D. matrix[1, 2]

43 What is Type Promotion in Method Overloading?

A. Converting an object to a primitive.
B. Automatically converting a smaller primitive type to a larger one to match a method signature.
C. Changing the return type of a method.
D. Promoting a private method to public.

44 Which StringBuilder method is used to insert characters at a specific index?

A. add()
B. append()
C. insert()
D. put()

45 What is the output of System.out.println("Apple".substring(1, 3));?

A. Ap
B. pp
C. ppl
D. App

46 Can you define a method inside a Java class that has the same name as the class?

A. No, that causes a compilation error.
B. Yes, but it is treated as a constructor.
C. Yes, provided it has a return type.
D. Yes, but only if it is static.

47 What happens if you omit the access modifier for a class member?

A. It becomes public.
B. It becomes private.
C. It takes on 'package-private' (default) access.
D. It becomes protected.

48 What is the mathematical complexity of accessing an element in an array by index?

A.
B.
C.
D.

49 Which statement correctly copies an array src to dest?

A. dest = src.clone();
B. System.arraycopy(src, 0, dest, 0, src.length);
C. dest = Arrays.copyOf(src, src.length);
D. All of the above.

50 Given StringBuilder sb = new StringBuilder("Java");, what does sb.reverse().toString() return?

A. "Java"
B. "avaJ"
C. "JavaJava"
D. Runtime Error