Code Snippets-based MCQs
Making Decisions (Conditionals)
Table of contents
Easy Level MCQs
-
What will be the output of the following code?
int num = 5; if (num > 0) { System.out.println("Positive"); }
Answer
Positive
-
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"); }
Answer
Non-positive
-
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"); }
Answer
b is greater
-
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"); }
Answer
Even
-
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"); }
Answer
Odd
-
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"); }
Answer
Equal
-
What will be the output of the following code?
int age = 18; if (age >= 18) { System.out.println("Adult"); } else { System.out.println("Minor"); }
Answer
Adult
-
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"); }
Answer
Positive
-
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"); }
Answer
C
-
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; }
Answer
Wednesday
-
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; }
Answer
Friday
-
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"); }
Answer
a is greater and positive
-
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"); }
Answer
Non-positive number
-
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"); }
Answer
Zero
-
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"); }
Answer
y is greater
-
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; }
Answer
April
-
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"); }
Answer
Divisible by 3 and Divisible by 5
-
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);
Answer
10
-
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"); }
Answer
Between 5 and 10
-
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"); }
Answer
Divisible by 2 and 3
-
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; }
Answer
Eleven
-
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; }
Answer
Third Quarter
-
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; }
Answer
Thursday
-
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"); }
Answer
a is divisible by b
-
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"); }
Answer
Grade B
-
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"); }
Answer
Not eligible to vote
-
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"); }
Answer
Positive
-
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; }
Answer
Wednesday
-
What will be the output of the following code?
int score = 45; if (score >= 50) { System.out.println("Pass"); } else { System.out.println("Fail"); }
Answer
Fail
-
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"); }
Answer
Warm
Slightly harder MCQs
-
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.
-
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"); } } }
Answer
Bark
-
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"); } } }
Answer
It’s a car
-
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"); } } }
Answer
Large rectangle
-
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"); } } }
Answer
Moderate salary
-
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"); } } }
Answer
High result
-
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(); } }
Answer
Hello, stranger!
-
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"); } } }
Answer
Large circle
-
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"); } } }
Answer
Expensive book
-
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()); } }
Answer
B
-
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"); } } }
Answer
The lamp is off
Tricky MCQs
-
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()); } }
Answer
20
-
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(); } }
Answer
Class B
-
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"); } } }
Answer
Product
-
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"); } } }
Answer
Negative result
-
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()); } }
Answer
100
-
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(); } }
Answer
20
-
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"); } } }
Answer
Derived class
-
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(); } }
Answer
Child
-
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(); } }
Answer
Meow
-
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"); } } }
Answer
Advanced calculation