Java questions and answers
OOP, strings, collections and exceptions. Practise java questions from the technical & programming section of placement tests, with a worked explanation after every answer. Practice is free and needs no sign-up.
Sample java questions with worked answers
Question 1
Which Java keyword, when applied to a class, prevents any other class from extending it?
- static
- final
- private
- abstract
Answer: B. final
Explanation: A final class cannot be subclassed (java.lang.String is an example). abstract means the opposite: the class is meant to be extended.
Question 2
Which of the following statements about interfaces is true in Java 8 and later?
- An interface can have instance fields that each object gets its own copy of
- An interface can contain default methods that have a body
- An interface can have a public constructor
- A class can implement only one interface
Answer: B. An interface can contain default methods that have a body
Explanation: Since Java 8, interfaces may define default and static methods with bodies. Interface fields are implicitly public static final, interfaces cannot have constructors, and a class can implement many interfaces.
Question 3
What will be the output of the following Java program?
class P {
static String who() { return "P"; }
String me() { return "p"; }
}
class C extends P {
static String who() { return "C"; }
String me() { return "c"; }
}
public class Main {
public static void main(String[] args) {
P obj = new C();
System.out.println(obj.who() + obj.me());
}
}
- Cc
- Pp
- Pc
- Cp
Answer: C. Pc
Explanation: Static methods are not overridden, only hidden, so obj.who() is chosen by the reference type P and returns "P". me() is an instance method, so it is overridden and returns "c".
All 30 java questions with answers
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- Which Java keyword, when applied to a class, prevents any other class from…
- What will be the output of the following Java program?
- What is the default value of an int instance variable (field) that is declared…
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program (default JVM settings)?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- Which of the following is NOT a primitive data type in Java?
- What is the size of the int data type in Java, regardless of the platform?
- What will be the output of the following Java program?
- Which access modifier makes a class member accessible only within the class in…
- What will be the output of the following Java program?
- Which of the following statements about interfaces is true in Java 8 and later?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- Which Java collection class does NOT allow duplicate elements and keeps…
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
- What will be the output of the following Java program?
Java: common questions
What are java questions?
OOP, strings, collections and exceptions. They appear in the technical & programming section of many placement tests.
Where can I practise java questions with answers for free?
Right here on LogicWiz Exams. Every java question has a worked explanation after you answer. Practice is free and needs no sign-up.
How should I practise java?
Start with easy questions, then move to medium and hard. Read the explanation after each answer, and come back to the ones you missed.
Start practising
On this page you can start a fresh set of java questions, choose easy, medium or hard, and time yourself if you want to. Or practise the whole subject: Technical & Programming practice.
More technical & programming topics
- Pseudo-code: Trace loops, recursion and bitwise logic.
- C Programming: Output prediction, pointers and operators.
- Python: Data types, comprehensions and mutability.
- Data Structures: Arrays, stacks, queues, trees, graphs and hashing.
- Algorithms: Complexity, sorting, searching, recursion and DP.
- Object-Oriented Programming: Encapsulation, inheritance, polymorphism, abstraction.
- DBMS: Keys, normalisation, transactions and indexing.
- SQL: Joins, grouping, subqueries and query output.
- Operating Systems: Processes, scheduling, memory and deadlocks.
- Computer Networks: OSI/TCP-IP, protocols, addressing and ports.
- Software Engineering: SDLC, testing, agile and version control.
- Cloud & DevOps: Service models, containers, CI/CD and Linux.
- GenAI Basics: LLMs, tokens, prompting, embeddings and RAG.
Other subjects
- Quantitative Aptitude: Arithmetic, percentages, time and work, speed, probability and data interpretation: the numerical section of every fresher test.
- Logical Reasoning: Series, coding-decoding, arrangements, syllogisms and puzzles: the section that separates prepared candidates from the rest.
- Verbal Ability: Vocabulary, grammar, reading comprehension and sentence order: tested in nearly every IT services hiring exam.
- Python: Python quizzes by topic, from types and strings to classes and generators: output prediction and interview concepts.
- SQL: SQL quizzes by topic, from SELECT and joins to window functions: query output and concepts for analyst and developer roles.
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge · Test from a job description · LogicWiz Open: free team quiz · Free interview prep