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

The Collection Interface.

  The Collection Interface. 

OracleJDK vs OpenJDK

Oracle JDK (Java Development Kit): Oracle JDK is the official reference implementation of the Java Platform, Standard Edition (Java SE). It included the JRE along with development tools. OpenJDK: An open-source alternative to Oracle JDK, OpenJDK is a community-driven project. It provides a free and open-source implementation of the Java Platform, and many other JDKs, including Oracle JDK, are derived from OpenJDK. Below is a simple table highlighting some key points of comparison between Oracle JDK and OpenJDK: Feature Oracle JDK OpenJDK Vendor Oracle Corporation OpenJDK Community Licensing Commercial (Paid) with Oracle Binary Code License Agreement Open Source (GNU General Public License, version 2, with the Classpath Exception) Support Commercial support available with Oracle Support subscription Community support, may have commercial support options from other vendors Updates and Patches Regular updates with security patches provided by Oracle Updates and patches contributed by the ...