Skip to main content

Java Exception Handling MCQ Test

  Loading…

Basic Java Program for Operators, Variables, and Control Flow Statements

 

1. Variables and Operators in Java

A variable is used to store data, and operators perform operations on these variables.

Example: Arithmetic Operators

public class BasicOperators { public static void main(String[] args) { int a = 10, b = 5; System.out.println("Sum: " + (a + b)); System.out.println("Difference: " + (a - b)); System.out.println("Product: " + (a * b)); System.out.println("Quotient: " + (a / b)); System.out.println("Remainder: " + (a % b)); } }

Explanation:

  • + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulo) are used to perform arithmetic calculations.

2. Control Flow Statements in Java

Control flow statements decide the execution path of the program based on conditions.

Example: If-Else Statement
public class IfElseExample { public static void main(String[] args) { int num = 10; if (num > 0) { System.out.println("The number is positive."); } else { System.out.println("The number is not positive."); } } }

Explanation:

  • if checks a condition; if true, it executes the block inside { }.
  • If false, it moves to else block.

Example: Switch Case
public class SwitchExample { public static void main(String[] args) { int day = 2; switch (day) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; default: System.out.println("Other day"); } } }

Explanation:

  • The switch statement is used when we have multiple possible values for a variable.
  • break ensures the program exits after finding the correct case.

Example: Loops (for, while, do-while)

For Loop:

public class ForLoopExample { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { System.out.println("Count: " + i); } } }

Explanation:

  • for loop repeats execution from i = 1 to i = 5.

While Loop:

public class WhileLoopExample { public static void main(String[] args) { int i = 1; while (i <= 5) { System.out.println("Count: " + i); i++; } } }

Explanation:

  • while loop runs until the condition (i <= 5) becomes false.

Do-While Loop:

public class DoWhileExample { public static void main(String[] args) { int i = 1; do { System.out.println("Count: " + i); i++; } while (i <= 5); } }

Explanation:

  • The do-while loop ensures the code runs at least once, even if the condition is false.

Summary:

Variables store values.
Operators perform calculations.
If-else decides execution based on a condition.
Switch handles multiple choices.
Loops repeat tasks multiple times.

These basics are the foundation of Java programming! 🚀

Comments

Popular posts from this blog

Iterators and Collections

In Java, iterators are objects that allow for sequential access to the elements of a collection. The Java Collections Framework provides the Iterator interface, which defines methods for iterating over collections such as lists, sets, and maps. Here's an explanation of iterators and their relationship with collections, along with examples: Iterator Interface: The Iterator interface provides methods to iterate over the elements of a collection sequentially: - boolean hasNext(): Returns true if there are more elements to iterate over. - E next(): Returns the next element in the iteration. - void remove():  Removes the last element returned by `next()` from the underlying collection (optional operation). Collections and Iterators: 1. Collection Interface:    - Collections represent groups of objects, such as lists, sets, and maps.    - They provide methods for adding, removing, and accessing elements. 2. Iterator Usage:    - Collections implement the Iter...

The Collection Interface.

  The Collection Interface.