Java

Java is a versatile, object-oriented language widely used for applications ranging from enterprise software to Android apps. It runs on the Java Virtual Machine (JVM), making programs portable across platforms. Java emphasizes readability, structure, and memory safety with automatic garbage collection. Programs are saved as .java files and compiled into bytecode. This chapter covers the essentials: printing output, variables, conditions, loops, functions, and concludes with a subtraction calculator example.

Hello world

A simple program in Java defines a class and a main method as the entry point.

public class Main {
  public static void main(String[] args) {
    System.out.println("Hello, world!");
  }
}

Variables and types

Java uses strong typing: int for integers, double for decimals, char for characters, boolean for true/false, and String for text.

int age = 22;
double gpa = 3.8;
char grade = 'A';
boolean isActive = true;
String name = "Alice";

Conditions

Conditional statements in Java rely on if, else, and switch. Every condition must be a boolean expression.

int score = 68;
if (score >= 50) {
  System.out.println("Pass");
} else {
  System.out.println("Fail");
}

Loops

Java loops include for, while, and do...while. They are structured similarly to C and C#.

for (int i = 1; i <= 3; i++) {
  System.out.println("Count: " + i);
}

Functions

In Java, functions are defined as methods inside classes. Each method specifies a return type and parameters.

static int multiply(int a, int b) {
  return a * b;
}

Final example — calculator for subtraction

The following Java program reads two numbers, subtracts them, and prints the difference. Save as Program.java and compile with javac Program.java, then run using java Program.

Program.java

import java.util.Scanner;

public class Program {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double minuend, subtrahend, difference;
    System.out.print("Enter minuend: ");
    minuend = sc.nextDouble();
    System.out.print("Enter subtrahend: ");
    subtrahend = sc.nextDouble();
    difference = minuend - subtrahend;
    System.out.println("Difference: " + difference);
    sc.close();
  }
}

MCQs

1. What is the correct file extension for Java source files?

(a) .java

(b) .class

(c) .jav

(d) .jv

► (a) .java

2. Which keyword is used to define a class in Java?

(a) define

(b) object

(c) class

(d) struct

► (c) class

3. Which method is the entry point of a Java program?

(a) start()

(b) init()

(c) main()

(d) run()

► (c) main()

4. Which statement prints text in Java?

(a) print()

(b) System.out.println()

(c) echo()

(d) printf()

► (b) System.out.println()

5. Which data type stores a decimal number in Java?

(a) int

(b) float

(c) double

(d) Both (b) and (c)

► (d) Both (b) and (c)

6. What is the default value of a boolean variable in Java?

(a) true

(b) false

(c) 0

(d) null

► (b) false

7. Which symbol starts a single-line comment in Java?

(a) #

(b) //

(c) /* */

(d) --

► (b) //

8. What does the new keyword do in Java?

(a) Creates a new variable

(b) Creates a new object

(c) Deletes an object

(d) Initializes a package

► (b) Creates a new object

9. Which loop is guaranteed to run at least once?

(a) for

(b) while

(c) do...while

(d) foreach

► (c) do...while

10. Which operator is used for equality comparison in Java?

(a) =

(b) ==

(c) !=

(d) equals()

► (b) ==

11. Which package must be imported to use the Scanner class?

(a) java.io

(b) java.util

(c) java.lang

(d) java.scanner

► (b) java.util

12. Which statement is used to exit a loop immediately?

(a) stop

(b) quit

(c) break

(d) end

► (c) break

13. Which keyword is used to skip the rest of a loop iteration?

(a) skip

(b) continue

(c) break

(d) next

► (b) continue

14. What is true about Java arrays?

(a) Index starts at 1

(b) Index starts at 0

(c) They grow automatically

(d) They can only store characters

► (b) Index starts at 0

15. Which access modifier allows members to be accessed everywhere?

(a) private

(b) protected

(c) internal

(d) public

► (d) public

16. Which class is the root of the Java class hierarchy?

(a) Main

(b) Object

(c) Base

(d) Root

► (b) Object

17. Which keyword is used to inherit a class in Java?

(a) this

(b) inherits

(c) extends

(d) implements

► (c) extends

18. In the subtraction calculator, which variable stores the result?

(a) output

(b) result

(c) difference

(d) sub

► (c) difference