What is the purpose of the static keyword in Java?
Correct answer:
B) It allows a variable to be shared across all instances of a class.
Explanation: The static keyword allows a variable to be shared across all instances of a class.
What is the purpose of the static keyword in Java?
Correct answer:
B) It allows a variable to be shared across all instances of a class.
Explanation: The static keyword allows a variable to be shared across all instances of a class.
Which of the following is a method of the String class?
Correct answer:
A) split()
Explanation: The split() method is a valid method of the String class used to divide a string into substrings.
How do you declare a single-dimensional array in Java?
Correct answer:
A) int arr[] = new int[10];
Explanation: The correct way to declare and initialize a single-dimensional array is int arr[] = new int[10]; .
Which of the following is a valid way to define a method in Java?
Correct answer:
B) void myMethod() {}
Explanation: The correct way to define a method is void myMethod() {} .
What is the output of System.out.println(10 % 3); ?
Correct answer:
B) 1
Explanation: The modulus operator % returns the remainder of the division, which is 1 in this case.
Which of the following types can be used in a switch statement?
Correct answer:
D) All of the above
Explanation: You can use int , String , char , and other types that are compatible with switch statements.
How do you handle exceptions in Java?
Correct answer:
A) using try-catch blocks
Explanation: Exceptions are handled in Java using try-catch blocks.
What is an ArrayList in Java?
Correct answer:
B) A resizable array implementation of the List interface
Explanation: An ArrayList is a resizable array implementation of the List interface.
Which of the following is not a Java access modifier?
Correct answer:
C) package
Explanation: package is not an access modifier in Java; the correct term is package-private, which is the default access level.
How do you create a thread in Java?
Correct answer:
C) Both A and B
Explanation: You can create a thread by either extending the Thread class or implementing the Runnable interface.
What is the output of System.out.println(3 * 2 + 1); ?
Correct answer:
A) 7
Explanation: The expression evaluates to 7 due to operator precedence.
Which of the following is a valid way to declare a constant variable in Java?
Correct answer:
D) Both A and C
Explanation: Both final int CONSTANT; and static final int CONSTANT = 100; are valid ways to declare constants.
How do you check if a String is empty in Java?
Correct answer:
D) All of the above
Explanation: All methods can be used to check if a string is empty.
Which exception is thrown when you divide by zero in Java?
Correct answer:
A) ArithmeticException
Explanation: An ArithmeticException is thrown when dividing by zero.
What is the purpose of the default keyword in an interface?
Correct answer:
B) To provide a default implementation of a method
Explanation: The default keyword provides a default implementation for a method in an interface.
Which of the following collections allows duplicate elements?
Correct answer:
B) List
Explanation: A List allows duplicate elements, while a Set does not.
What will the following code output? System.out.println(“Hello”.charAt(1)); ?
Correct answer:
B) e
Explanation: The charAt(1) method returns the character at index 1, which is 'e'.
How do you create an immutable class?
Correct answer:
A) By using private fields and no setter methods
Explanation: An immutable class is typically created using private fields and no setter methods.
Which of the following is not a type of loop in Java?
Correct answer:
D) repeat-until
Explanation: repeat-until is not a loop type in Java.
What is the purpose of the try block?
Correct answer:
B) To execute code that may throw an exception
Explanation: The try block is used to execute code that may throw an exception.
How do you create a synchronized method in Java?
Correct answer:
B) public synchronized void myMethod() {}
Explanation: The correct way to declare a synchronized method is public synchronized void myMethod() {} .
What is the result of System.out.println(5 + “5”); ?
Correct answer:
B) 55
Explanation: The expression evaluates to "55" due to string concatenation.
Which of the following is not a primitive data type in Java?
Correct answer:
C) String
Explanation: String is not a primitive data type; it is a reference type.
How can you declare an abstract method in a class?
Correct answer:
A) abstract void myMethod();
Explanation: An abstract method is declared with the abstract keyword and has no body.
What will happen if you try to access a null reference in Java?
Correct answer:
B) It throws a NullPointerException.
Explanation: Accessing a null reference results in a NullPointerException .
Which of the following is a method to remove an element from a List?
Correct answer:
A) remove(index)
Explanation: The remove(index) method is used to remove an element from a List.
What is the output of System.out.println(“Hello World”.substring(0, 5)); ?
Correct answer:
A) Hello
Explanation: The substring(0, 5) method extracts characters from index 0 to 4, resulting in "Hello".
How do you declare an interface in Java?
Correct answer:
D) Both A and B
Explanation: Both interface MyInterface {} and public interface MyInterface {} are valid ways to declare an interface.
Which of the following is true about constructors in Java?
Correct answer:
D) Both A and C
Explanation: Constructors have the same name as the class and are called when an object is created.
What is the purpose of the final keyword in Java?
Correct answer:
D) All of the above
Explanation: The final keyword can prevent method overriding, inheritance, and can be used for immutable variables.
Which method is used to convert a string to an integer?
Correct answer:
D) Both A and C
Explanation: Both Integer.valueOf(str); and Integer.parseInt(str); convert a string to an integer.
What does the public keyword signify in Java?
Correct answer:
A) The member can be accessed from anywhere.
Explanation: The public keyword allows access to the member from anywhere.
How do you find the size of a List in Java?
Correct answer:
B) list.size();
Explanation: The size() method returns the number of elements in a List.
What is the output of System.out.println(“Java”.equals(“java”)); ?
Correct answer:
B) false
Explanation: The equals() method is case-sensitive, so "Java" is not equal to "java".
Which of the following is a valid constructor declaration?
Correct answer:
C) public MyConstructor() {}
Explanation: A constructor does not have a return type, and it must match the class name.
How do you define a method that can throw an exception?
Correct answer:
A) void myMethod() throws Exception {}
Explanation: You can define a method that throws an exception using throws in the method signature.
What is the result of System.out.println(7 / 2); ?
Correct answer:
B) 3
Explanation: Integer division truncates the decimal part, resulting in 3.
Which of the following will create an empty HashMap?
Correct answer:
D) All of the above
Explanation: All options are valid ways to create an empty HashMap.
How do you check if a map contains a specific key?
Correct answer:
A) map.contains(key);
Explanation: The containsKey(key) method checks if a map contains a specific key.
Which of the following is not a feature of Java?
Correct answer:
D) Multiple inheritance
Explanation: Java does not support multiple inheritance directly through classes.
What is the purpose of the this keyword in Java?
Correct answer:
B) To reference the current object
Explanation: The this keyword is used to reference the current object.
How do you declare a variable that can hold a decimal value in Java?
Correct answer:
B) double myDecimal;
Explanation: The double data type is used to declare a variable that can hold decimal values.
Which of the following statements is true about the final keyword?
Correct answer:
A) It can be applied to methods, variables, and classes.
Explanation: The final keyword can be applied to methods, variables, and classes to restrict modifications.
What is the output of System.out.println(2 * 3 + 4 * 5); ?
Correct answer:
B) 26
Explanation: The expression evaluates to 26 based on the order of operations.
How do you convert a primitive type to a wrapper type in Java?
Correct answer:
A) Autoboxing
Explanation: The process of converting a primitive type to a wrapper type is known as autoboxing.
What is the output of System.out.println(“2” + 3 + 1); ?
Correct answer:
B) 231
Explanation: The expression evaluates to "231" due to string concatenation.
Which of the following is true about an interface in Java?
Correct answer:
C) It can extend multiple interfaces.
Explanation: An interface can extend multiple interfaces, and it cannot have instance variables or implementations of methods.
How do you make a class final in Java?
Correct answer:
A) By using the final keyword before the class name
Explanation: A class is made final by using the final keyword before the class name.
What is the output of System.out.println(5 + “10”); ?
Correct answer:
B) 510
Explanation: The expression evaluates to "510" due to string concatenation.
Which of the following is a feature of exception handling in Java?
Correct answer:
A) It allows for error recovery.
Explanation: Exception handling allows for error recovery in a program.
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.