Code Snippets-based MCQs

Making Decisions (Conditionals)

Table of contents
  1. Easy Level MCQs
  2. Slightly harder MCQs
  3. Tricky MCQs

Easy Level MCQs

  1. What will be the output of the following code?

    int num = 5;
    if (num > 0) {
        System.out.println("Positive");
    }
    
    Positive
    Negative
    0
    Compilation error
    Answer

    Positive

  2. What will be the output of the following code?

    int num = -3;
    if (num > 0) {
        System.out.println("Positive");
    } else {
        System.out.println("Non-positive");
    }
    
    Positive
    Non-positive
    0
    Compilation error
    Answer

    Non-positive

  3. What will be the output of the following code?

    int a = 10;
    int b = 20;
    if (a > b) {
        System.out.println("a is greater");
    } else {
        System.out.println("b is greater");
    }
    
    a is greater
    b is greater
    a is equal to b
    Compilation error
    Answer

    b is greater

  4. What will be the output of the following code?

    int num = 10;
    if (num % 2 == 0) {
        System.out.println("Even");
    } else {
        System.out.println("Odd");
    }
    
    Even
    Odd
    None
    Compilation error
    Answer

    Even

  5. What will be the output of the following code?

    int num = 7;
    if (num % 2 == 0) {
        System.out.println("Even");
    } else {
        System.out.println("Odd");
    }
    
    Even
    Odd
    None
    Compilation error
    Answer

    Odd

  6. What will be the output of the following code?

    int a = 15;
    int b = 15;
    if (a == b) {
        System.out.println("Equal");
    } else {
        System.out.println("Not Equal");
    }
    
    Equal
    Not Equal
    Greater
    Compilation error
    Answer

    Equal

  7. What will be the output of the following code?

    int age = 18;
    if (age >= 18) {
        System.out.println("Adult");
    } else {
        System.out.println("Minor");
    }
    
    Adult
    Minor
    Senior
    Compilation error
    Answer

    Adult

  8. What will be the output of the following code?

    int a = 5;
    if (a < 0) {
        System.out.println("Negative");
    } else if (a > 0) {
        System.out.println("Positive");
    } else {
        System.out.println("Zero");
    }
    
    Negative
    Positive
    Zero
    Compilation error
    Answer

    Positive

  9. What will be the output of the following code?

    int score = 75;
    if (score >= 90) {
        System.out.println("A");
    } else if (score >= 80) {
        System.out.println("B");
    } else if (score >= 70) {
        System.out.println("C");
    } else {
        System.out.println("D");
    }
    
    A
    B
    C
    D
    Answer

    C

  10. What will be the output of the following code?

    int day = 3;
    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");
            break;
    }
    
    Monday
    Tuesday
    Wednesday
    Other
    Answer

    Wednesday

  11. What will be the output of the following code?

    int day = 5;
    switch (day) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        default:
            System.out.println("Other");
            break;
    }
    
    Monday
    Thursday
    Friday
    Other
    Answer

    Friday

  12. What will be the output of the following code?

    int a = 8;
    int b = 3;
    if (a > b && a > 0) {
        System.out.println("a is greater and positive");
    } else {
        System.out.println("Condition not met");
    }
    
    a is greater and positive
    Condition not met
    a is greater but not positive
    Compilation error
    Answer

    a is greater and positive

  13. What will be the output of the following code?

    int a = -5;
    if (a < 0 || a == 0) {
        System.out.println("Non-positive number");
    } else {
        System.out.println("Positive number");
    }
    
    Non-positive number
    Positive number
    Zero
    Compilation error
    Answer

    Non-positive number

  14. What will be the output of the following code?

    int num = 0;
    if (num > 0) {
        System.out.println("Positive");
    } else if (num < 0) {
        System.out.println("Negative");
    } else {
        System.out.println("Zero");
    }
    
    Positive
    Negative
    Zero
    Compilation error
    Answer

    Zero

  15. What will be the output of the following code?

    int x = 10;
    int y = 20;
    if (x > y) {
        System.out.println("x is greater");
    } else if (x < y) {
        System.out.println("y is greater");
    } else {
        System.out.println("x and y are equal");
    }
    
    x is greater
    y is greater
    x and y are equal
    Compilation error
    Answer

    y is greater

  16. What will be the output of the following code?

    int month = 4;
    switch (month) {
        case 1:
            System.out.println("January");
            break;
        case 2:
            System.out.println("February");
            break;
        case 3:
            System.out.println("March");
            break;
        case 4:
            System.out.println("April");
            break;
        default:
            System.out.println("Other");
            break;
    }
    
    January
    February
    April
    Other
    Answer

    April

  17. What will be the output of the following code?

    int num = 15;
    if (num % 3 == 0) {
        System.out.println("Divisible by 3");
    }
    if (num % 5 == 0) {
        System.out.println("Divisible by 5");
    }
    
    Divisible by 3
    Divisible by 5
    Divisible by 3
    Divisible by 3 and Divisible by 5
    Answer

    Divisible by 3 and Divisible by 5

  18. What will be the output of the following code?

    int a = 5;
    int b = 10;
    int max;
    if (a > b) {
        max = a;
    } else {
        max = b;
    }
    System.out.println(max);
    
    5
    10
    0
    Compilation error
    Answer

    10

  19. What will be the output of the following code?

    int num = 6;
    if (num > 5) {
        if (num < 10) {
            System.out.println("Between 5 and 10");
        } else {
            System.out.println("10 or more");
        }
    } else {
        System.out.println("5 or less");
    }
    
    Between 5 and 10
    10 or more
    5 or less
    Compilation error
    Answer

    Between 5 and 10

  20. What will be the output of the following code?

    int a = 12;
    if (a % 2 == 0) {
        if (a % 3 == 0) {
            System.out.println("Divisible by 2 and 3");
        } else {
            System.out.println("Divisible by 2 but not by 3");
        }
    } else {
        System.out.println("Not divisible by 2");
    }
    
    Divisible by 2 and 3
    Divisible by 2 but not by 3
    Not divisible by 2
    Compilation error
    Answer

    Divisible by 2 and 3

  21. What will be the output of the following code?

    int num = 11;
    switch (num) {
        case 10:
            System.out.println("Ten");
            break;
        case 11:
            System.out.println("Eleven");
            break;
        case 12:
            System.out.println("Twelve");
            break;
        default:
            System.out.println("Other");
            break;
    }
    
    Ten
    Eleven
    Twelve
    Other
    Answer

    Eleven

  22. What will be the output of the following code?

    int month = 7;
    switch (month) {
        case 1:
        case 2:
        case 3:
            System.out.println("First Quarter");
            break;
        case 4:
        case 5:
        case 6:
            System.out.println("Second Quarter");
            break;
        case 7:
        case 8:
        case 9:
            System.out.println("Third Quarter");
            break;
        case 10:
        case 11:
        case 12:
            System.out.println("Fourth Quarter");
            break;
        default:
            System.out.println("Invalid Month");
            break;
    }
    
    First Quarter
    Second Quarter
    Third Quarter
    Fourth Quarter
    Answer

    Third Quarter

  23. What will be the output of the following code?

    int day = 5;
    switch (day) {
        case 1:
            System.out.println("Sunday");
            break;
        case 2:
            System.out.println("Monday");
            break;
        case 3:
            System.out.println("Tuesday");
            break;
        case 4:
            System.out.println("Wednesday");
            break;
        case 5:
            System.out.println("Thursday");
            break;
        default:
            System.out.println("Invalid day");
            break;
    }
    
    Sunday
    Tuesday
    Thursday
    Invalid day
    Answer

    Thursday

  24. What will be the output of the following code?

    int a = 4;
    int b = 2;
    if (a % b == 0) {
        System.out.println("a is divisible by b");
    } else {
        System.out.println("a is not divisible by b");
    }
    
    a is divisible by b
    a is not divisible by b
    Compilation error
    Runtime error
    Answer

    a is divisible by b

  25. What will be the output of the following code?

    int score = 85;
    if (score >= 90) {
        System.out.println("Grade A");
    } else if (score >= 80) {
        System.out.println("Grade B");
    } else if (score >= 70) {
        System.out.println("Grade C");
    } else if (score >= 60) {
        System.out.println("Grade D");
    } else {
        System.out.println("Grade F");
    }
    
    Grade A
    Grade B
    Grade C
    Grade D
    Answer

    Grade B

  26. What will be the output of the following code?

    int age = 16;
    if (age >= 18) {
        System.out.println("Eligible to vote");
    } else {
        System.out.println("Not eligible to vote");
    }
    
    Eligible to vote
    Not eligible to vote
    Compilation error
    Runtime error
    Answer

    Not eligible to vote

  27. What will be the output of the following code?

    int number = 10;
    if (number > 0) {
        System.out.println("Positive");
    } else if (number < 0) {
        System.out.println("Negative");
    } else {
        System.out.println("Zero");
    }
    
    Positive
    Negative
    Zero
    Compilation error
    Answer

    Positive

  28. What will be the output of the following code?

    int day = 3;
    switch (day) {
        case 1:
            System.out.println("Monday");
            break;
        case 2:
            System.out.println("Tuesday");
            break;
        case 3:
            System.out.println("Wednesday");
            break;
        case 4:
            System.out.println("Thursday");
            break;
        case 5:
            System.out.println("Friday");
            break;
        default:
            System.out.println("Weekend");
            break;
    }
    
    Monday
    Wednesday
    Friday
    Weekend
    Answer

    Wednesday

  29. What will be the output of the following code?

    int score = 45;
    if (score >= 50) {
        System.out.println("Pass");
    } else {
        System.out.println("Fail");
    }
    
    Pass
    Fail
    Compilation error
    Runtime error
    Answer

    Fail

  30. What will be the output of the following code?

    int temperature = 30;
    if (temperature > 35) {
        System.out.println("Hot");
    } else if (temperature > 25) {
        System.out.println("Warm");
    } else {
        System.out.println("Cold");
    }
    
    Hot
    Warm
    Cold
    Compilation error
    Answer

    Warm

Back to Top

Slightly harder MCQs

  1. Consider the following implementation of a class Square:

    public class Square
    {
       private int sideLength; 
       private int area; // Not a good idea 
    
       public Square(int length)
       {
          sideLength = length;
       } 
       public int getArea() {
          area = sideLength * sideLength; return area;
       } 
    }
    

    Why is it not a good idea to introduce an instance variable for the area? Rewrite the class so that area is a local variable.

  2. What will be the output of the following code?

     class Animal {
         void sound() {
             System.out.println("Some sound");
         }
     }
    
     class Dog extends Animal {
         void sound() {
             System.out.println("Bark");
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Animal animal = new Dog();
             if (animal instanceof Dog) {
                 animal.sound();
             } else {
                 System.out.println("Unknown sound");
             }
         }
     }
    
    Some sound
    Bark
    Unknown sound
    Compilation error
    Answer

    Bark

  3. What will be the output of the following code?

     class Vehicle {
         String type;
    
         Vehicle(String type) {
             this.type = type;
         }
    
         void display() {
             System.out.println("Vehicle type: " + type);
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Vehicle vehicle = new Vehicle("Car");
             if (vehicle.type.equals("Car")) {
                 System.out.println("It's a car");
             } else {
                 System.out.println("It's not a car");
             }
         }
     }
    
    Vehicle type: Car
    It's a car
    It's not a car
    Compilation error
    Answer

    It’s a car

  4. What will be the output of the following code?

     class Rectangle {
         int width, height;
    
         Rectangle(int width, int height) {
             this.width = width;
             this.height = height;
         }
    
         int area() {
             return width * height;
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Rectangle rect = new Rectangle(5, 10);
             if (rect.area() > 50) {
                 System.out.println("Large rectangle");
             } else {
                 System.out.println("Small rectangle");
             }
         }
     }
    
    Large rectangle
    Small rectangle
    Compilation error
    Runtime error
    Answer

    Large rectangle

  5. What will be the output of the following code?

     class Employee {
         String name;
         double salary;
    
         Employee(String name, double salary) {
             this.name = name;
             this.salary = salary;
         }
    
         void raiseSalary(double percentage) {
             salary += salary * (percentage / 100);
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Employee emp = new Employee("John", 50000);
             emp.raiseSalary(10);
             if (emp.salary > 55000) {
                 System.out.println("High salary");
             } else {
                 System.out.println("Moderate salary");
             }
         }
     }
    
    High salary
    Moderate salary
    Compilation error
    Runtime error
    Answer

    Moderate salary

  6. What will be the output of the following code?

     class Calculator {
         int add(int a, int b) {
             return a + b;
         }
    
         int multiply(int a, int b) {
             return a * b;
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Calculator calc = new Calculator();
             int result = calc.add(5, 10) * calc.multiply(2, 3);
             if (result > 50) {
                 System.out.println("High result");
             } else {
                 System.out.println("Low result");
             }
         }
     }
    
    High result
    Low result
    Compilation error
    Runtime error
    Answer

    High result

  7. What will be the output of the following code?

     class Person {
         String name;
    
         Person(String name) {
             this.name = name;
         }
    
         void greet() {
             if (name.equals("Alice")) {
                 System.out.println("Hello, Alice!");
             } else {
                 System.out.println("Hello, stranger!");
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Person p = new Person("Bob");
             p.greet();
         }
     }
    
    Hello, Alice!
    Hello, stranger!
    Compilation error
    Runtime error
    Answer

    Hello, stranger!

  8. What will be the output of the following code?

     class Circle {
         double radius;
    
         Circle(double radius) {
             this.radius = radius;
         }
    
         double area() {
             return Math.PI * radius * radius;
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Circle circle = new Circle(3);
             if (circle.area() > 20) {
                 System.out.println("Large circle");
             } else {
                 System.out.println("Small circle");
             }
         }
     }
    
    Large circle
    Small circle
    Compilation error
    Runtime error
    Answer

    Large circle

  9. What will be the output of the following code?

     class Book {
         String title;
         double price;
    
         Book(String title, double price) {
             this.title = title;
             this.price = price;
         }
    
         boolean isExpensive() {
             return price > 500;
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Book book = new Book("Java Programming", 600);
             if (book.isExpensive()) {
                 System.out.println("Expensive book");
             } else {
                 System.out.println("Affordable book");
             }
         }
     }
    
    Expensive book
    Affordable book
    Compilation error
    Runtime error
    Answer

    Expensive book

  10. What will be the output of the following code?

     class Student {
         String name;
         int marks;
    
         Student(String name, int marks) {
             this.name = name;
             this.marks = marks;
         }
    
         String getGrade() {
             if (marks >= 90) {
                 return "A";
             } else if (marks >= 80) {
                 return "B";
             } else if (marks >= 70) {
                 return "C";
             } else {
                 return "F";
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Student student = new Student("Tom", 85);
             System.out.println(student.getGrade());
         }
     }
    
    A
    B
    C
    F
    Answer

    B

  11. What will be the output of the following code?

    class Lamp {
        boolean isOn;
    
        Lamp(boolean isOn) {
            this.isOn = isOn;
        }
    
        void switchState() {
            isOn = !isOn;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            Lamp lamp = new Lamp(true);
            lamp.switchState();
            if (lamp.isOn) {
                System.out.println("The lamp is on");
            } else {
                System.out.println("The lamp is off");
            }
        }
    }
    
    The lamp is on
    The lamp is off
    Compilation error
    Runtime error
    Answer

    The lamp is off

Back to Top

Tricky MCQs

  1. What will be the output of the following code?

     class Parent {
         int value = 10;
    
         int getValue() {
             return value;
         }
     }
    
     class Child extends Parent {
         int value = 20;
    
         int getValue() {
             if (value > super.value) {
                 return value;
             } else {
                 return super.value;
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Parent p = new Child();
             System.out.println(p.getValue());
         }
     }
    
    10
    20
    Compilation error
    Runtime error
    Answer

    20

  2. What will be the output of the following code?

     class A {
         static String name = "Class A";
    
         static void printName() {
             System.out.println(name);
         }
     }
    
     class B extends A {
         static String name = "Class B";
    
         static void printName() {
             if (name.equals("Class A")) {
                 super.printName();
             } else {
                 System.out.println(name);
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             B.printName();
         }
     }
    
    Class A
    Class B
    Compilation error
    Runtime error
    Answer

    Class B

  3. What will be the output of the following code?

     class Base {
         int calculate(int a, int b) {
             return a + b;
         }
     }
    
     class Derived extends Base {
         int calculate(int a, int b) {
             return a * b;
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Base base = new Derived();
             int result = base.calculate(5, 10);
             if (result == 15) {
                 System.out.println("Sum");
             } else {
                 System.out.println("Product");
             }
         }
     }
    
    Sum
    Product
    Compilation error
    Runtime error
    Answer

    Product

  4. What will be the output of the following code?

     class Alpha {
         int method(int x) {
             return x * 2;
         }
     }
    
     class Beta extends Alpha {
         int method(int x) {
             if (x > 0) {
                 return x * 3;
             } else {
                 return super.method(x);
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Beta beta = new Beta();
             int result = beta.method(-5);
             if (result < 0) {
                 System.out.println("Negative result");
             } else {
                 System.out.println("Positive result");
             }
         }
     }
    
    Negative result
    Positive result
    Compilation error
    Runtime error
    Answer

    Negative result

  5. What will be the output of the following code?

     class X {
         int value = 100;
    
         int getValue() {
             return value;
         }
     }
    
     class Y extends X {
         int value = 200;
    
         int getValue() {
             if (value > super.value) {
                 return value - super.value;
             } else {
                 return super.value - value;
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             X obj = new Y();
             System.out.println(obj.getValue());
         }
     }
    
    100
    200
    0
    Compilation error
    Answer

    100

  6. What will be the output of the following code?

     class Outer {
         int x = 10;
    
         class Inner {
             int x = 20;
    
             void display() {
                 if (x > Outer.this.x) {
                     System.out.println(x);
                 } else {
                     System.out.println(Outer.this.x);
                 }
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Outer.Inner inner = new Outer().new Inner();
             inner.display();
         }
     }
    
    10
    20
    30
    Compilation error
    Answer

    20

  7. What will be the output of the following code?

     class Base {
         String getName() {
             return "Base";
         }
     }
    
     class Derived extends Base {
         String getName() {
             return "Derived";
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Base base = new Derived();
             if (base.getName().equals("Derived")) {
                 System.out.println("Derived class");
             } else {
                 System.out.println("Base class");
             }
         }
     }
    
    Base class
    Derived class
    Compilation error
    Runtime error
    Answer

    Derived class

  8. What will be the output of the following code?

     class Parent {
         void method() {
             System.out.println("Parent");
         }
     }
    
     class Child extends Parent {
         void method() {
             if (true) {
                 System.out.println("Child");
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Parent p = new Child();
             p.method();
         }
     }
    
    Parent
    Child
    Compilation error
    Runtime error
    Answer

    Child

  9. What will be the output of the following code?

     class Animal {
         void makeSound() {
             System.out.println("Some sound");
         }
     }
    
     class Cat extends Animal {
         void makeSound() {
             if (this instanceof Cat) {
                 System.out.println("Meow");
             } else {
                 super.makeSound();
             }
         }
     }
    
     public class Main {
         public static void main(String[] args) {
             Animal animal = new Cat();
             animal.makeSound();
         }
     }
    
    Some sound
    Meow
    Compilation error
    Runtime error
    Answer

    Meow

  10. What will be the output of the following code?

    class Calculator {
        int add(int a, int b) {
            return a + b;
        }
    }
    
    class AdvancedCalculator extends Calculator {
        int add(int a, int b) {
            return super.add(a, b) * 2;
        }
    
        int add(int a, int b, int c) {
            return this.add(a, b) + c;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            AdvancedCalculator calc = new AdvancedCalculator();
            int result = calc.add(2, 3, 4);
            if (result == 10) {
                System.out.println("Simple addition");
            } else {
                System.out.println("Advanced calculation");
            }
        }
    }
    
    Simple addition
    Advanced calculation
    Compilation error
    Runtime error
    Answer

    Advanced calculation

Back to Top