1Which of the following creates an infinite loop in Java?
A.for(;;)
B.while(true)
C.do { } while(true);
D.All of the above
Correct Answer: All of the above
Explanation:All three constructs syntax are valid ways to create an infinite loop. for(;;) has empty initialization, condition, and increment fields, defaulting the condition to true. while(true) and do-while(true) explicitly test against true.
Incorrect! Try again.
2What 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
Correct Answer: 1
Explanation:A do-while loop executes the body at least once before checking the condition. Therefore, is incremented from 0 to 1, the condition checks false, and the loop terminates.
Incorrect! Try again.
3Which 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.
Correct Answer: It iterates through elements sequentially without using an explicit counter.
Explanation:The for-each loop (introduced in Java 5) simplifies iteration over arrays and collections. It does not provide access to the index and cannot modify the collection structure safely during iteration.
Incorrect! Try again.
4What 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;
Correct Answer: int[][] array = new int[3][4];
Explanation:Java uses brackets for each dimension. The syntax type[][] name = new type[rows][cols] is the standard way to declare and initialize a 2D array.
Incorrect! Try again.
5Consider the array declaration: int[] arr = new int[5];. What is the default value stored at index 0?
A.null
B.
C.undefined
D.-1
Correct Answer:
Explanation:When an array of primitives is allocated, elements are initialized to their default values. For int, the default value is $0$.
Incorrect! Try again.
6Which property is used to determine the size of an array named numbers?
A.numbers.length()
B.numbers.size()
C.numbers.length
D.numbers.count
Correct Answer: numbers.length
Explanation:Arrays in Java have a final public field called length that holds the size of the array. length() is a method for Strings, and size() is usually for Collections.
Incorrect! Try again.
7What 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.
Correct Answer: It throws an ArrayIndexOutOfBoundsException.
Explanation:Valid indices for an array of size range from $0$ to . Accessing index 5 in an array of size 5 results in a runtime exception.
Incorrect! Try again.
8Which 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.
Correct Answer: The varargs parameter must be the last parameter in the method signature.
Explanation:Java allows variable arguments (...) to simplify method calls. However, to avoid ambiguity, the varargs parameter must always be the last parameter in the list.
Incorrect! Try again.
9How 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.
Correct Answer: As a method taking an array int[] numbers.
Explanation:Internally, Java compiles varargs into an array. The parameter int... numbers is treated exactly like int[] numbers inside the method body.
Incorrect! Try again.
10Given the following Enum:
java
enum Day { MONDAY, TUESDAY }
What does Day.MONDAY.ordinal() return?
A."MONDAY"
B.1
C.
D.Day
Correct Answer:
Explanation:The ordinal() method returns the position of the enum constant in the declaration, starting from $0$. Since MONDAY is first, it returns 0.
Incorrect! Try again.
11Which 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()
Correct Answer: values()
Explanation:The compiler automatically generates a static values() method for every enum, which returns an array containing all enum constants in the order they were declared.
Incorrect! Try again.
12Can 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.
Correct Answer: Yes, but it must be private or package-private.
Explanation:Enums can have constructors to initialize fields, but they cannot be public or protected because enum instances are controlled by the class itself, not instantiated externally.
Incorrect! Try again.
13What 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.
Correct Answer: To initialize the newly created object.
Explanation:Memory allocation is handled by the new keyword. The constructor is called immediately after allocation to initialize the object's state (fields).
Incorrect! Try again.
14If 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.
Correct Answer: The compiler generates a default no-argument constructor.
Explanation:If no constructors are explicitly defined in a class, the Java compiler inserts a default no-argument constructor that calls super().
Incorrect! Try again.
15Identify the invalid method declaration:
A.void method() { }
B.int method(int a) { return a; }
C.method() { }
D.static void method() { }
Correct Answer: method() { }
Explanation:A method declaration must include a return type (or void). Only constructors do not have a return type. method() { } lacks a return type.
Incorrect! Try again.
16Which 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.
Correct Answer: Methods with the same name but different parameter lists within the same class.
Explanation:Overloading occurs when multiple methods in the same class share the same name but have different parameter lists (number, type, or order of parameters).
Incorrect! Try again.
17Can 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.
Correct Answer: No, never.
Explanation:Java distinguishes overloaded methods by their signature (name + parameter types). The return type is not part of the signature, so changing only the return type creates ambiguity.
Incorrect! Try again.
18What 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.
Correct Answer: To refer to the current object instance.
Explanation:this is a reference variable that refers to the current object invoking the method or constructor.
Incorrect! Try again.
19In a constructor, how do you call another constructor of the same class?
A.super()
B.call()
C.this()
D.constructor()
Correct Answer: this()
Explanation:The syntax this(arguments) is used to call another constructor within the same class. This is known as constructor chaining.
Incorrect! Try again.
20Where 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.
Correct Answer: It must be the first statement.
Explanation:If used, this() (or super()) must be the very first statement in a constructor body to ensure proper initialization order.
Incorrect! Try again.
21When 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.
Correct Answer: Before the constructor is invoked, every time an object is created.
Explanation:Instance initializer blocks ({ ... } inside a class) run every time an instance is created. The compiler copies the block into every constructor, immediately after the super-constructor call.
Incorrect! Try again.
22When 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.
Correct Answer: Only once, when the class is loaded by the ClassLoader.
Explanation:Static blocks (static { ... }) are executed once when the class is first initialized/loaded into the JVM, typically to initialize static variables.
Incorrect! Try again.
23Which 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.
Correct Answer: They are immutable.
Explanation:Once a String object is created, its data cannot be changed. Any modification methods return a new String object.
Incorrect! Try again.
24What 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
Correct Answer: false
Explanation:s1 refers to a literal in the String Pool. s2 refers to a new object on the Heap. The == operator compares references (memory addresses), which are different.
Incorrect! Try again.
25Which method is used to compare the content of two String objects?
A.==
B.compareTo()
C.equals()
D.contentEquals()
Correct Answer: equals()
Explanation:While == compares references, the .equals() method in the String class is overridden to compare the actual character sequences of the strings.
Incorrect! Try again.
26Which 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
Correct Answer: StringBuilder
Explanation:StringBuilder is mutable and not synchronized, making it faster and more memory-efficient for concatenating or modifying strings compared to the immutable String class.
Incorrect! Try again.
27What is the default capacity of a StringBuilder when initialized with no arguments?
A.
B.10
C.16
D.32
Correct Answer: 16
Explanation:The default constructor new StringBuilder() creates a builder with no characters in it and an initial capacity of 16 characters.
Incorrect! Try again.
28How 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.
Correct Answer: int[][] arr = new int[3][];
Explanation:A ragged array is an array of arrays where sub-arrays can be of different lengths. You define the number of rows first (new int[3][]), leaving the column size empty to be defined individually later.
Incorrect! Try again.
29What output does System.out.println(10 + 20 + "Java"); produce?
A.1020Java
B.30Java
C.Java1020
D.Java30
Correct Answer: 30Java
Explanation:Evaluation is left-to-right. First 10 + 20 results in integer 30. Then 30 + "Java" performs string concatenation, resulting in "30Java".
Incorrect! Try again.
30Which operator is used to check if an object is an instance of a specific class?
A.typeof
B.instanceof
C.isA
D.as
Correct Answer: instanceof
Explanation:The instanceof binary operator is used to test if an object is an instance of a specific class or implements an interface.
Incorrect! Try again.
31Consider 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.
Correct Answer: ++x increments then uses the value; x++ uses the value then increments.
Explanation:++x is pre-increment (increment immediately). x++ is post-increment (return original value, then increment).
Incorrect! Try again.
32What 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)
Correct Answer: s.charAt(2)
Explanation:Java Strings are not arrays, so bracket notation [] does not work. The method charAt(int index) returns the character at the specified index.
Incorrect! Try again.
33What 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.
Correct Answer: Only within the for loop block.
Explanation:Variables declared in the initialization part of a for loop have loop scope. They are not accessible outside the loop body.
Incorrect! Try again.
34Which keyword is used to stop the execution of the current loop immediately?
A.stop
B.return
C.break
D.continue
Correct Answer: break
Explanation:The break statement terminates the closest enclosing loop (or switch statement) immediately. continue skips the rest of the current iteration.
Incorrect! Try again.
35Given 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
Correct Answer: 5
Explanation:Arrays are objects. b = a copies the reference, not the content. Both variables point to the same array object in memory.
Incorrect! Try again.
36Which 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)
Correct Answer: public static void main(String[] args)
Explanation:The JVM looks for public static void main(String[] args) (or varargs String... args) to start execution.
Incorrect! Try again.
37In 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.
Correct Answer: A runtime instance of a class.
Explanation:A class is the blueprint/template, and an object is a concrete instance of that class created at runtime occupying memory.
Incorrect! Try again.
38Can 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.
Correct Answer: No.
Explanation:this refers to the current object. Static methods belong to the class, not a specific object instance, so there is no this reference available.
Incorrect! Try again.
39What is the result of String s = " Hello "; s.trim(); if we print s afterward?
A."Hello"
B." Hello "
C."Hello "
D.null
Correct Answer: " Hello "
Explanation:Strings are immutable. s.trim() returns a new string with whitespace removed, but it does not modify s in place. Since the result wasn't reassigned to s, s remains unchanged.
Incorrect! Try again.
40Which of the following is a valid constructor for a class named Car?
A.public void Car() {}
B.public car() {}
C.Car() {}
D.new Car() {}
Correct Answer: Car() {}
Explanation:A constructor must have the exact same name as the class and no return type. void Car() is treated as a method, not a constructor. car() has the wrong casing.
Incorrect! Try again.
41In 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.
Correct Answer: The number of rows.
Explanation:A 2D array is an array of arrays. table.length gives the number of sub-arrays (rows) it contains.
Incorrect! Try again.
42How 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]
Correct Answer: matrix[1][2]
Explanation:Indices are 0-based. The 2nd row is index 1, and the 3rd column is index 2.
Incorrect! Try again.
43What 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.
Correct Answer: Automatically converting a smaller primitive type to a larger one to match a method signature.
Explanation:If an exact match for an argument isn't found, Java tries to promote the type (e.g., int to long, or float to double) to find a matching overloaded method.
Incorrect! Try again.
44Which StringBuilder method is used to insert characters at a specific index?
A.add()
B.append()
C.insert()
D.put()
Correct Answer: insert()
Explanation:append() adds to the end, while insert(int offset, ...) inserts data at the specified position.
Incorrect! Try again.
45What is the output of System.out.println("Apple".substring(1, 3));?
A.Ap
B.pp
C.ppl
D.App
Correct Answer: pp
Explanation:substring(start, end) includes the character at start but excludes the character at end. Index 1 is 'p', Index 2 is 'p', Index 3 is 'l' (excluded). Result: "pp".
Incorrect! Try again.
46Can 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.
Correct Answer: Yes, provided it has a return type.
Explanation:If a member has the same name as the class but includes a return type, it is a standard method, not a constructor. However, this is considered bad practice as it is confusing.
Incorrect! Try again.
47What 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.
Correct Answer: It takes on 'package-private' (default) access.
Explanation:Without a modifier, the member is accessible only to classes within the same package.
Incorrect! Try again.
48What is the mathematical complexity of accessing an element in an array by index?
A.
B.
C.
D.
Correct Answer:
Explanation:Array access is a constant time operation because the memory address is calculated directly using the base address and the index offset.
Incorrect! Try again.
49Which 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.
Correct Answer: All of the above.
Explanation:All listed options are valid ways to create a copy of an array (either a shallow copy of the object or copying elements).
Incorrect! Try again.
50Given StringBuilder sb = new StringBuilder("Java");, what does sb.reverse().toString() return?
A."Java"
B."avaJ"
C."JavaJava"
D.Runtime Error
Correct Answer: "avaJ"
Explanation:The reverse() method modifies the StringBuilder sequence by reversing the order of characters. "Java" becomes "avaJ".
Incorrect! Try again.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.