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

Why is String[] args necessary in main() method in Java?

  Why is String[] args necessary in main() method in Java? In Java, the main method serves as the entry point for the program. The correct syntax for the main method is: public static void main (String[] args) { System.out.println( "Hello, Java!" ); } 🔹 Breaking it down: public → Accessible from anywhere. static → No need to create an object of the class to run it. void → No return value. main → Special method recognized by the JVM as the starting point. String[] args → Stores command-line arguments (optional but required by JVM). Why Can't We Skip String[] args ? JVM looks for main(String[] args) When you run a Java program, the JVM searches for the main method with exactly this signature : public static void main (String[] args) If you change or remove String[] args , the JVM cannot find the correct main() method and throws a runtime error (NoSuchMethodError). Java Specification Requires It The Java Language Specification (JLS) defines that main...