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

Passing and Returning Objects in Java Methods

Passing and Returning Objects in Java Methods In Java, objects can be passed as parameters to methods and returned from methods just like other primitive data types. This allows for flexibility and the manipulation of object state within methods. Let's explore how passing and returning objects work in Java. Passing Objects as Parameters When you pass an object as a parameter to a method, you are essentially passing a reference to that object. This means that changes made to the object inside the method will affect the original object outside the method.  Example: class Car {     String model;     Car(String model) {         this.model = model;     } } public class CarProcessor {     // Method to modify the Car object     static void modifyCar(Car car, String newModel) {         car.model = newModel;     }     public static void main(String[] args) {       ...

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 ...