What does the break statement do in a switch-case structure?
Correct answer:
A) Terminates the switch statement
Explanation: The break statement terminates the switch-case structure, preventing fall-through to the next case.
What does the break statement do in a switch-case structure?
Correct answer:
A) Terminates the switch statement
Explanation: The break statement terminates the switch-case structure, preventing fall-through to the next case.
Which of the following is a valid way to compare two strings for equality?
Correct answer:
D) Both A and C
Explanation: Both str1.equals(str2) and str1.compareTo(str2) == 0 are valid methods for comparing strings for equality.
What will be the output of System.out.println(5 * 2 + 3); ?
Correct answer:
B) 13
Explanation: The expression evaluates to 10 (5 * 2) + 3 = 13.
How can you declare a method that takes no arguments?
Correct answer:
A) public void methodName() { … }
Explanation: The correct syntax to declare a method that takes no arguments is public void methodName() { ... } .
What is the result of the following operation: int[] arr = {1, 2, 3}; arr[1] = 5; System.out.println(arr[1]); ?
Correct answer:
D) 5
Explanation: The second element (index 1) of the array is updated to 5, so the output is 5
Which of the following statements correctly creates a new object of a class named Car ?
Correct answer:
A) Car myCar = new Car();
Explanation: The correct syntax to create a new object is Car myCar = new Car(); .
How do you declare a method that returns an integer value?
Correct answer:
D) public int methodName() { … }
Explanation: The correct syntax to declare a method that returns an integer is public int methodName() { ... } .
What is the purpose of the import statement?
Correct answer:
B) To include other classes and packages
Explanation: The import statement is used to include classes and packages from other libraries.
How do you create a multi-dimensional array in Java?
Correct answer:
C) Both A and B
Explanation: Both int[][] arr = new int[3][4]; and int arr[][] = new int[3][4]; are valid ways to declare a multi-dimensional array.
What will happen if you try to access an index outside the bounds of an array?
Correct answer:
C) It throws an ArrayIndexOutOfBoundsException
Explanation: Accessing an index that is out of bounds will throw an ArrayIndexOutOfBoundsException.
What does the continue statement do in a loop?
Correct answer:
B) Skips the current iteration and proceeds to the next
Explanation: The continue statement skips the current iteration of the loop and continues with the next iteration.
What will be the output of System.out.println(“Hello”.length()); ?
Correct answer:
A) 5
Explanation: The length() method returns the number of characters in the string, which is 5 for "Hello".
How do you convert a string to an integer in Java?
Correct answer:
C) Both A and B
Explanation: Both Integer.valueOf(str); and Integer.parseInt(str); can convert a string to an integer.
Which of the following is the correct way to define an interface in Java?
Correct answer:
D) Both B and C
Explanation: An interface can be defined using either public interface MyInterface { ... } or interface MyInterface { ... } .
What is the main purpose of the final keyword?
Correct answer:
C) To define a constant value
Explanation: The final keyword is used to declare a constant value that cannot be modified.
How do you check if a string is empty in Java?
Correct answer:
D) All of the above
Explanation: All options are valid ways to check if a string is empty in Java.
Which of the following statements is used to handle exceptions in Java?
Correct answer:
A) try-catch
Explanation: The try-catch statement is used to handle exceptions in Java.
What is the output of the following code snippet? System.out.println(7 % 4);
Correct answer:
A) 3
Explanation: The % operator returns the remainder of the division, which is 3.
How do you define a constructor in Java?
Correct answer:
A) It must have the same name as the class and no return type.
Explanation: A constructor must have the same name as the class and does not have a return type.
Which of the following keywords is used to create a subclass in Java?
Correct answer:
A) extends
Explanation: The extends keyword is used to create a subclass from a superclass.
What is the output of System.out.println(“abc”.charAt(1)); ?
Correct answer:
B) b
Explanation: The charAt(1) method returns the character at index 1, which is 'b'.
Which of the following statements is true regarding arrays in Java?
Correct answer:
D) Both B and C
Explanation: Arrays in Java are fixed in size and can also be multi-dimensional.
What is the purpose of the super keyword?
Correct answer:
A) To call a superclass constructor
Explanation: The super keyword is used to call a constructor or method from a superclass.
What does the default keyword indicate in a switch statement?
Correct answer:
A) A value to be used if none of the cases match
Explanation: The default keyword is used to specify a block of code that executes if no case matches in a switch statement.
What is the purpose of the throws keyword in method declarations?
Correct answer:
B) To declare exceptions that may be thrown
Explanation: The throws keyword is used in method declarations to indicate that a method may throw exceptions.
How can you concatenate two strings in Java?
Correct answer:
C) Both A and B
Explanation: You can concatenate strings using both the concat() method and the + operator.
Which of the following is the correct syntax for an enhanced for loop?
Correct answer:
A) for (Type var : collection) { … }
Explanation: The enhanced for loop syntax is for (Type var : collection) { ... } .
What does the instanceof operator do in Java?
Correct answer:
A) Checks if an object is an instance of a specific class or subclass
Explanation: The instanceof operator checks if an object is an instance of a specific class or subclass.
How can you make a variable accessible across multiple classes?
Correct answer:
B) By declaring it as public
Explanation: Declaring a variable as public allows it to be accessible from multiple classes.
What is the purpose of a constructor overloading in Java?
Correct answer:
D) All of the above
Explanation: Constructor overloading allows multiple constructors with different parameter types, including default constructors without parameters.
Which of the following statements is used to declare a package in Java?
Correct answer:
A) package com.example;
Explanation: The package statement is used to declare a package in Java.
How do you throw an exception manually in Java?
Correct answer:
A) throw new Exception(“Error message”);
Explanation: The correct syntax to throw an exception manually is throw new Exception("Error message"); .
What keyword is used to prevent a class from being subclassed?
Correct answer:
A) final
Explanation: The final keyword prevents a class from being subclassed.
How do you retrieve the size of an ArrayList in Java?
Correct answer:
A) arrayList.size();
Explanation: The size() method retrieves the number of elements in an ArrayList.
What will happen if you try to modify a string in Java?
Correct answer:
B) A new string will be created
Explanation: Strings in Java are immutable; modifying a string will create a new string instead of changing the original.
Which of the following is a valid declaration of an enum in Java?
Correct answer:
A) enum Colors { RED, GREEN, BLUE }
Explanation: The correct way to declare an enum is enum Colors { RED, GREEN, BLUE } .
How can you handle multiple exceptions in a single catch block?
Correct answer:
D) Both A and B
Explanation: Both catch (IOException | SQLException e) { ... } and separate catch blocks handle multiple exceptions.
What will be the output of System.out.println(10 + 5 + ” is the sum.”); ?
Correct answer:
A) 15 is the sum.
Explanation: The expression evaluates to 15, resulting in "15 is the sum." due to string concatenation.
Which method is used to convert a string to uppercase?
Correct answer:
A) str.toUpperCase()
Explanation: The toUpperCase() method converts a string to uppercase.
How can you remove an element from an ArrayList?
Correct answer:
A) arrayList.remove(index);
Explanation: The remove(index) method is used to remove an element from an ArrayList.
What will be the output of System.out.println(3 + 4 + “Java”); ?
Correct answer:
A) 7Java
Explanation: The expression evaluates to "7Java" due to string concatenation.
What is the purpose of the finally block in Java?
Correct answer:
D) Both A and B
Explanation: The finally block always executes after the try-catch blocks and is typically used for resource cleanup.
How do you compare two objects for reference equality?
Correct answer:
A) ==
Explanation: The == operator checks for reference equality, determining if two object references point to the same object.
Which keyword is used to define a constant variable in Java?
Correct answer:
B) final
Explanation: The final keyword is used to define a constant variable that cannot be modified.
What is the default value of an uninitialized int variable in Java?
Correct answer:
A) 0
Explanation: The default value of an uninitialized int variable in Java is 0.
How do you handle an unchecked exception in Java?
Correct answer:
B) You can ignore it
Explanation: Unchecked exceptions (like RuntimeExceptions) do not need to be caught or declared.
Which of the following methods can be used to iterate over elements in a List?
Correct answer:
D) All of the above
Explanation: You can iterate over elements in a List using a standard for loop, an enhanced for loop, or an iterator.
What is the purpose of the static keyword?
Correct answer:
A) To declare a class variable that can be accessed without an object
Explanation: The static keyword declares a class variable that can be accessed without creating an object of the class.
How do you convert a string to a character array in Java?
Correct answer:
A) str.toCharArray()
Explanation: The toCharArray() method converts a string to a character array.
What is the purpose of the volatile keyword in Java?
Correct answer:
A) To indicate that a variable may be changed unexpectedly
Explanation: The volatile keyword indicates that a variable's value may be changed by different threads unexpectedly, ensuring visibility of its changes.
If this exam is giving you a hard time, let us be your helping hand. Contact us, and we'll provide the assistance you need to pass and move forward.