Java

Java is a widely-used, class-based, object-oriented programming language designed to have as few implementation dependencies as possible. It allows developers to write once and run anywhere (WORA). In this chapter, we will learn about variables, data types, operators, control statements, loops, methods, classes, objects, and a final example in Java.

Introduction to Java

Java programs are written in files with .java extension. The entry point of every Java program is the main method inside a class. Java is strongly typed and strictly enforces object-oriented principles.

Variables and Data Types

Java supports primitive types such as int, double, float, char, boolean, and String objects. Variables must be declared with their type before use.

int age = 25;<br>
double height = 5.8;<br>
char grade = 'A';<br>
String name = "Riya";<br>
boolean isStudent = true;<br>
>

Operators

Java supports arithmetic (+, -, *, /, %), relational (>, <, >=, <=, ==, !=), logical (&&, ||, !), and assignment (=, +=, -=, etc.) operators for manipulating data.

Control Statements

Conditional statements like if, else if, else, and switch are used to perform different actions based on conditions.

Loops

Java provides for, while, and do-while loops for repeated execution of statements. for-each loop is used with arrays and collections.

Methods

Methods are blocks of code that perform a specific task. They can return values or be void. Syntax includes return type, method name, and parameters.

Classes and Objects

Classes are blueprints for objects. An object is an instance of a class. The new keyword creates objects. Access modifiers like public, private, and protected control visibility.

Final Example — Collatz Sequence

The following example demonstrates the Collatz sequence in Java. Save as Collatz.java.

import java.util.Scanner;<br>
public class Collatz {<br>
  public static void main(String[] args)<br>
  {<br>
    Scanner sc = new Scanner(System.in);<br>
    System.out.print("Enter a number: ");<br>
    int n = sc.nextInt();<br>
    while(n != 1)<br>
    {<br>
      System.out.print(n + " ");<br>
      if(n % 2 == 0) n /= 2;<br>
      else n = 3*n + 1;<br>
    }
    System.out.println("1");
  }
}
>

MCQs

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

(a) object

(b) class

(c) def

(d) struct

► (b) class

2. What is the entry point of a Java program?

(a) start()

(b) main()

(c) init()

(d) run()

► (b) main()

3. Which keyword is used to create objects in Java?

(a) create

(b) new

(c) make

(d) object

► (b) new

4. What is the default value of an uninitialized boolean in Java?

(a) true

(b) false

(c) 0

(d) null

► (b) false

5. Which of these is a valid primitive type in Java?

(a) number

(b) int

(c) string

(d) list

► (b) int

6. Which operator is used for logical AND in Java?

(a) &

(b) &&

(c) ||

(d) AND

► (b) &&

7. Which loop is guaranteed to execute at least once?

(a) for

(b) while

(c) do-while

(d) foreach

► (c) do-while

8. Which method is used to get user input from console in Java?

(a) System.in()

(b) Scanner.next()

(c) Scanner(System.in)

(d) input()

► (c) Scanner(System.in)

9. Which keyword is used for inheritance in Java?

(a) implement

(b) inherit

(c) extends

(d) super

► (c) extends

10. How do you declare an array of integers in Java?

(a) int arr[];

(b) int[] arr;

(c) Both (a) and (b)

(d) array int arr;

► (c) Both (a) and (b)

11. Which keyword prevents a class from being inherited?

(a) static

(b) final

(c) const

(d) immutable

► (b) final

12. Which keyword is used to handle exceptions?

(a) catch

(b) try

(c) finally

(d) All of the above

► (d) All of the above

13. Which access modifier allows a member to be accessible within the same package?

(a) public

(b) private

(c) protected

(d) default

► (d) default

14. Which method is used to start a thread in Java?

(a) run()

(b) start()

(c) execute()

(d) begin()

► (b) start()

15. Which of the following is used to compare objects in Java?

(a) ==

(b) equals()

(c) compare()

(d) compareTo()

► (b) equals()

16. Which keyword is used to define a constant in Java?

(a) constant

(b) const

(c) final

(d) static

► (c) final

17. Which exception occurs when division by zero happens in Java?

(a) NullPointerException

(b) ArithmeticException

(c) IOException

(d) NumberFormatException

► (b) ArithmeticException

18. In the Collatz example, which loop is used to continue until n equals 1?

(a) for

(b) while

(c) do-while

(d) foreach

► (b) while