Java language, OOP, exceptions and threads
Topics: OOP concepts; Java features, JVM, bytecode; data types, classes, inheritance, interfaces, packages; exceptions, multithreading, I/O, applets and AWT; .NET basics.
Java and the JVM
Java source compiles to bytecode that runs on any Java Virtual Machine: “write once, run anywhere”. Primitive types have fixed sizes: byte 1, short 2, int 4, long 8, float 4, double 8, char 2 (Unicode). Integer division truncates; Strings are immutable objects; garbage collection frees unused memory.
- Bytecode + JVM = portability
- char is 2 bytes (Unicode)
- Strings are immutable
Example: What do 7/2 and 7/2.0 give in Java?
- Both int: integer division gives 3.
- With a double: 3.5.
Answer: 3 and 3.5
Example: Why is Java called platform independent?
- javac produces bytecode, not machine code.
- Any machine with a JVM can run the same bytecode.
Answer: Bytecode runs on any JVM
Classes, inheritance and interfaces
A class extends one superclass but can implement many interfaces. Overloading (same name, different parameters) is resolved at compile time; overriding (same signature in a subclass) at run time, so a superclass reference calls the subclass’s method. super calls the parent’s constructor or method; final prevents overriding or inheritance.
- Single inheritance of classes, many interfaces
- Overriding resolved at run time
- final: no override / no subclass
Example: class A { void show(){ print("A"); } } class B extends A { void show(){ print("B"); } } — A x = new B(); x.show(); prints?
- The object is a B; overridden methods are chosen at run time.
- Prints B.
Answer: B
Example: How does Java get the effect of multiple inheritance?
- A class may extend only one class.
- It implements several interfaces.
Answer: Interfaces
Exceptions and threads
Exceptions separate error handling: try, catch, finally (which always runs, used to release resources), throw and throws. Checked exceptions (IOException) must be caught or declared; unchecked ones (ArithmeticException, NullPointerException) need not be. Threads (extend Thread or implement Runnable) run concurrently; synchronized methods prevent two threads corrupting shared data.
- finally always runs
- Checked vs unchecked
- synchronized for shared data
Example: Dividing an int by zero in Java throws which exception, checked or unchecked?
- Integer division by zero throws ArithmeticException.
- It is unchecked (a RuntimeException).
Answer: ArithmeticException, unchecked
Example: Does a finally block run when the try block returns?
- finally runs whether or not an exception occurs, even on return.
- Yes.
Answer: Yes
Java compiles to bytecode that the JVM runs, making it platform-independent. Each file has a class, and main is public static void main(String[] args). Encapsulation hides data, inheritance reuses it through extends, polymorphism overrides methods, and abstraction uses abstract classes and interfaces. A class extends one class but can implement many interfaces. final prevents change or overriding. Exceptions use try, catch, finally, throw and throws. Threads extend Thread or implement Runnable. Garbage collection reclaims memory automatically. .NET uses the CLR with MSIL as intermediate code.
- JDK includes JRE, which includes JVM.
- int is 4 bytes; char is 2 bytes (Unicode).
- Strings are immutable.
- super calls the parent class.
- public > protected > default > private access.
Key formulas
- javac A.java → A.class
- java A
- class B extends A implements I
Practice questions
Java bytecode is run by the
- JVM
- BIOS
- compiler
- linker
Show the answer
Answer: A. JVM Platform independence.
Java supports multiple inheritance through
- classes
- interfaces
- pointers
- macros
Show the answer
Answer: B. interfaces Implements many.
A class inherits with the keyword
- implements
- extends
- inherits
- super
Show the answer
Answer: B. extends One parent.
Java’s char size is
- 1 byte
- 2 bytes
- 4 bytes
- 8 bytes
Show the answer
Answer: B. 2 bytes Unicode.
Strings in Java are
- immutable
- always mutable
- primitive
- arrays of int
Show the answer
Answer: A. immutable StringBuilder is mutable.
finally runs
- always (normally)
- only on error
- never
- only at compile
Show the answer
Answer: A. always (normally) Clean-up.
Memory is reclaimed by
- free()
- garbage collection
- delete
- the user only
Show the answer
Answer: B. garbage collection Automatic.
A thread can be created by implementing
- Runnable
- Comparable
- Serializable
- Applet
Show the answer
Answer: A. Runnable Or extending Thread.
final on a method
- prevents overriding
- allows overriding
- makes it static
- deletes it
Show the answer
Answer: A. prevents overriding Fixed behaviour.
MSIL belongs to
- .NET
- Java only
- C
- BIOS
Show the answer
Answer: A. .NET Intermediate language.
The practice questions were written for this studio; they are not copied from ASTU papers. ASTU publishes no topic syllabus for these exams, so the topics are an informed guide based on diploma courses, not an official list.