IITIAN1997 STUDIO

Java programming and visual programming

JLEE CSE · 10 practice questions with answers

Practise and take a mock test →

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?

  1. Both int: integer division gives 3.
  2. With a double: 3.5.

Answer: 3 and 3.5

Example: Why is Java called platform independent?

  1. javac produces bytecode, not machine code.
  2. 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?

  1. The object is a B; overridden methods are chosen at run time.
  2. Prints B.

Answer: B

Example: How does Java get the effect of multiple inheritance?

  1. A class may extend only one class.
  2. 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?

  1. Integer division by zero throws ArithmeticException.
  2. It is unchecked (a RuntimeException).

Answer: ArithmeticException, unchecked

Example: Does a finally block run when the try block returns?

  1. finally runs whether or not an exception occurs, even on return.
  2. 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

  1. JVM
  2. BIOS
  3. compiler
  4. linker
Show the answer

Answer: A. JVM Platform independence.

Java supports multiple inheritance through

  1. classes
  2. interfaces
  3. pointers
  4. macros
Show the answer

Answer: B. interfaces Implements many.

A class inherits with the keyword

  1. implements
  2. extends
  3. inherits
  4. super
Show the answer

Answer: B. extends One parent.

Java’s char size is

  1. 1 byte
  2. 2 bytes
  3. 4 bytes
  4. 8 bytes
Show the answer

Answer: B. 2 bytes Unicode.

Strings in Java are

  1. immutable
  2. always mutable
  3. primitive
  4. arrays of int
Show the answer

Answer: A. immutable StringBuilder is mutable.

finally runs

  1. always (normally)
  2. only on error
  3. never
  4. only at compile
Show the answer

Answer: A. always (normally) Clean-up.

Memory is reclaimed by

  1. free()
  2. garbage collection
  3. delete
  4. the user only
Show the answer

Answer: B. garbage collection Automatic.

A thread can be created by implementing

  1. Runnable
  2. Comparable
  3. Serializable
  4. Applet
Show the answer

Answer: A. Runnable Or extending Thread.

final on a method

  1. prevents overriding
  2. allows overriding
  3. makes it static
  4. deletes it
Show the answer

Answer: A. prevents overriding Fixed behaviour.

MSIL belongs to

  1. .NET
  2. Java only
  3. C
  4. 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.

Practise and take a mock test →