CBSE Class 12 IT (802) — Unit 3: JAVA
Last-night revision sheet — Theory Q&A + MCQs | Sourabh, Krishnagar Public School
⚡ Priority order if you're short on time: OOP concepts → Data types/Operators → Control flow → Arrays → Exception Handling → Wrapper Classes →
Strings → Threads/Assertions
1. Introduction to Java & Java Fundamentals
Q1. What is Java?
A: Java is a high-level, object-oriented, platform-independent programming language developed by Sun Microsystems (now Oracle).
Q2. Why is Java called platform-independent?
A: Java code is compiled into bytecode (.class file), which can run on any machine having a JVM — "Write Once, Run Anywhere" (WORA).
Q3. What is JVM?
A: Java Virtual Machine — it converts bytecode into machine-specific code so Java programs can run on any OS.
Q4. What is JDK and JRE?
A: JDK (Java Development Kit) = tools to write, compile & run Java programs (includes JRE + compiler). JRE (Java Runtime Environment) =
only what's needed to run Java programs (includes JVM + libraries).
Q5. What are the features of Java?
A: Simple, Object-Oriented, Platform-independent, Secure, Robust, Multithreaded, Portable, High Performance.
Q6. What is bytecode?
A: An intermediate, platform-independent code generated by the Java compiler from source code; understood by the JVM.
Q7. Structure of a basic Java program?
public class Main {
public static void main(String[] args) {
// statements
}
}
2. Object-Oriented Programming (OOP)
Q8. What is OOP?
A: A programming approach based on the concept of "objects" that contain data (attributes) and code (methods), instead of just
functions and logic.
Q9. What is a Class?
A: A blueprint/template that defines the properties (variables) and behaviors (methods) an object will have. No memory is allocated for a
class.
Q10. What is an Object?
A: An instance of a class; a real-world entity created from the class blueprint. Memory is allocated when an object is created.
Q11. What are the 4 main pillars/principles of OOP?
A: Encapsulation, Inheritance, Polymorphism, Abstraction.
Q12. What is Encapsulation?
A: Binding data and methods together into a single unit (class), and restricting direct access to data using private variables with public
getter/setter methods.
Q13. What is Inheritance?
A: A mechanism where a child (derived) class acquires the properties and methods of a parent (base) class using the keyword extends .
Q14. What is Polymorphism?
A: The ability of a method/object to take many forms. Achieved via Method Overloading (compile-time) and Method Overriding (run-time).
Q15. What is Abstraction?
A: Hiding internal implementation details and showing only the essential features to the user (e.g., using abstract classes/interfaces).
Q16. Difference between Method Overloading and Overriding?
Overloading Overriding
Same class, same method name, different parameters Parent-child classes, same method name & same parameters
Compile-time polymorphism Run-time polymorphism
Q17. What is a Constructor?
A: A special method with the same name as the class, having no return type, automatically invoked when an object is created. Used to
initialize objects.
Q18. Types of constructors?
A: Default constructor (no arguments, auto-provided if none is defined) and Parameterized constructor (accepts arguments to initialize
values).
Q19. What is the this keyword?
A: Refers to the current object of the class; used to differentiate instance variables from parameters of the same name.
Q20. What is the super keyword?
A: Used in a child class to refer to its immediate parent class — to call parent constructors or methods.
3. Java Language Elements & Data Types
Q21. What are the primitive data types in Java?
A: byte, short, int, long, float, double, char, boolean.
Q22. What is a variable?
A: A named memory location used to store a value that can change during program execution.
Q23. Rules for naming identifiers in Java?
A: Must start with a letter, $ or _; cannot start with a digit; case-sensitive; cannot use reserved keywords.
Q24. What are literals?
A: Fixed/constant values directly assigned to a variable (e.g., 10, 'A', "Hello", true).
Q25. What is Type Casting?
A: Converting one data type into another. Widening (implicit — small to large, e.g. int to double) and Narrowing (explicit — large to small,
e.g. double to int, needs cast operator).
4. Operators
Q26. What are the types of operators in Java?
A: Arithmetic (+ - * / %), Relational (== != > < >= <=), Logical (&& || !), Assignment (= += -= etc.), Increment/Decrement (++ --),
Bitwise (& | ^ ~ << >>), Ternary (? :).
Q27. What is the Ternary operator? Give syntax.
A: A shorthand for if-else: variable = (condition) ? value_if_true : value_if_false;
Q28. Difference between == and = ?
A: == is a relational operator used for comparison; = is an assignment operator used to assign a value.
5. Control Flow Statements
Q29. What are the types of control statements in Java?
A: Selection (if, if-else, switch), Iteration/Looping (for, while, do-while), Jump (break, continue, return).
Q30. Difference between while and do-while loop?
A: In while , condition is checked before the loop body executes (may run 0 times). In do-while , the body executes first, then the
condition is checked (runs at least once).
Q31. What does the break statement do?
A: Terminates the loop/switch immediately and transfers control to the statement after it.
Q32. What does the continue statement do?
A: Skips the current iteration and moves to the next iteration of the loop.
Q33. Syntax of switch statement?
switch(expression) {
case value1: statement; break;
case value2: statement; break;
default: statement;
}
6. Arrays
Q34. What is an Array?
A: A collection of fixed number of elements of the same data type, stored in contiguous memory locations, accessed using an index
(starting from 0).
Q35. How do you declare and initialize an array?
int arr[] = new int[5]; // declaration with size
int arr[] = {10, 20, 30}; // declaration with initialization
Q36. What is the index range of an array of size n?
A: 0 to n-1.
Q37. What is a 2D array?
A: An array of arrays, used to represent data in rows and columns (like a matrix/table). Declared as: int arr[][] = new int[3][3];
Q38. How do you find the length of an array?
A: Using the .length property (no parentheses) — e.g., [Link] .
7. Class Design
Q39. What are instance variables?
A: Variables declared inside a class but outside any method; each object gets its own copy.
Q40. What are the access specifiers/modifiers in Java?
A: public (accessible everywhere), private (only within the same class), protected (within package + subclasses), Default/no
modifier (within same package only).
Q41. What is the static keyword used for?
A: A static member (variable/method) belongs to the class rather than any object; shared by all instances; can be accessed without
creating an object.
8. Exception Handling
Q42. What is an Exception?
A: An unwanted/unexpected event that disrupts the normal flow of a program during execution (a runtime error).
Q43. What is Exception Handling?
A: A mechanism to handle runtime errors gracefully using try , catch , finally , throw , and throws , so the program doesn't crash
abruptly.
Q44. Explain try, catch, and finally blocks.
A: try — block of code that might throw an exception. catch — catches and handles the exception. finally — always executes,
whether an exception occurs or not (used for cleanup).
try {
int a = 10/0;
} catch (ArithmeticException e) {
[Link]("Cannot divide by zero");
} finally {
[Link]("Executed always");
}
Q45. Difference between throw and throws ?
A: throw is used to explicitly throw a single exception. throws is used in a method signature to declare that a method might throw
exceptions.
Q46. Name common built-in exception types.
A: ArithmeticException, ArrayIndexOutOfBoundsException, NullPointerException, NumberFormatException, ClassNotFoundException.
9. Assertions
Q47. What is an Assertion?
A: A statement used to test assumptions about a program during development — checks whether a condition is true; if false, it throws an
AssertionError .
Q48. Syntax of assertion?
assert condition;
assert condition : errorMessage;
10. Threads
Q49. What is a Thread?
A: The smallest unit of a process; allows a program to perform multiple tasks concurrently (multithreading).
Q50. Ways to create a thread in Java?
A: By extending the Thread class, or by implementing the Runnable interface.
11. Wrapper Classes
Q51. What is a Wrapper Class?
A: A class that converts a primitive data type into an object (e.g., int → Integer, double → Double, char → Character, boolean → Boolean).
Q52. Why are wrapper classes needed?
A: To use primitives as objects (needed in collections), and to use utility methods like conversion between data types (e.g.,
[Link]() ).
12. String Manipulation
Q53. What is a String in Java?
A: A sequence of characters, treated as an object of the String class (immutable — once created, its value cannot be changed).
Q54. Important String methods to remember:
Method Use
length() Returns number of characters
charAt(i) Returns character at index i
substring(a,b)
Extracts part of string from index a to b-1
toUpperCase() / toLowerCase() Changes case
equals() Compares content of two strings
trim() Removes leading/trailing spaces
concat() Joins two strings
replace() Replaces characters/substrings
Q55. Difference between String and StringBuffer?
A: String is immutable (value can't change once created); StringBuffer is mutable (can be modified) and more efficient for repeated
modifications.
MCQ Practice — Unit 3: Java
1. Java is a:
a) Platform-dependent language b) Platform-independent language c) Only markup language d) None of these
Ans: b
2. Which of these converts Java bytecode to machine code?
a) JDK b) JRE c) JVM d) IDE
Ans: c
3. Which keyword is used for inheritance in Java?
a) implements b) extends c) inherits d) super
Ans: b
4. A constructor's name must be the same as the:
a) Method b) Class c) Package d) Variable
Ans: b
5. Which OOP concept restricts direct access to data?
a) Inheritance b) Polymorphism c) Encapsulation d) Abstraction
Ans: c
6. private members of a class are accessible:
a) Everywhere b) Only within the same class c) Only in subclass d) Only in package
Ans: b
7. Method overloading is resolved at:
a) Run time b) Compile time c) Load time d) None
Ans: b
8. Which loop executes at least once even if the condition is false?
a) for b) while c) do-while d) if-else
Ans: c
9. Array index in Java starts from:
a) 1 b) -1 c) 0 d) depends on array
Ans: c
10. Which keyword is used to handle exceptions along with try?
a) except b) catch c) throw only d) error
Ans: b
11. Which block always executes whether an exception occurs or not?
a) try b) catch c) finally d) throw
Ans: c
12. Wrapper class for 'int' is:
a) Int b) Integer c) IntWrap d) Number
Ans: b
13. Which method returns the number of characters in a string?
a) size() b) length() c) count() d) len()
Ans: b
14. Which class allows a program to run multiple tasks concurrently?
a) String b) Thread c) Array d) Wrapper
Ans: b
15. An assertion that fails throws a/an:
a) Exception b) AssertionError c) RuntimeError d) SyntaxError
Ans: b
16. Which operator is used for logical AND?
a) & b) && c) || d) !
Ans: b
17. String objects in Java are:
a) Mutable b) Immutable c) Both d) None
Ans: b
18. Default value of a boolean variable in Java is:
a) true b) false c) 0 d) null
Ans: b
19. Which statement is used to exit a loop immediately?
a) continue b) exit c) break d) return
Ans: c
20. Which of the following is NOT a primitive data type?
a) int b) String c) char d) boolean
Ans: b
Good luck tomorrow, Sourabh! Read every Q once tonight, then re-skim just the bold headers + MCQs before you sleep.