Code Snippets-based MCQs

Soft Intro to Java with BlueJ (Objects & Classes)

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

Easy Level MCQs

  1. Given a Person class with a constructor that initializes the name and age of a person and a method getName that returns the name, what will be the output of the following code snippet?
    Person p = new Person("Alice", 30);
    System.out.println(p.getName());
    
    Alice
    30
    null
    Compilation error
    Answer

    Alice

  2. Given a Rectangle class with a constructor that initializes its width and height, and a method area that returns the area of the rectangle, what will be the return value of the given method call?
    Rectangle rect = new Rectangle(3, 4);
    rect.area();
    
    7
    12
    0
    Compilation error
    Answer

    12

  3. What will be the output of the following code snippet?
    int a = 10;
    int b = 20;
    System.out.println(a + b + "" + a + a + "" + b);
    
    1020101020
    30101020
    302010
    10202010
    Answer

    30101020

  4. Given a Calculator class with a method addAndPrint that takes two integer parameters, adds them, and prints the result, what will be the return value of the given method call in the following code snippet?
    Calculator calc = new Calculator();
    calc.addAndPrint(7, 5);
    
    12
    75
    Compilation error
    0
    Answer

    12

  5. Given a Printer class with a method printGreeting that takes a String parameter and prints a greeting message including the provided name, what will be the return value of the given method call in the following code snippet?
    Printer printer = new Printer();
    printer.printGreeting("Alice");
    
    Hello, Alice!
    Hello, name!
    Alice
    Compilation error
    Answer

    Hello, Alice!

  6. What is the variable name in the following declaration?
    Car myCar = new Car("Toyota");
    
    Car
    myCar
    new
    Toyota
    Answer

    myCar

  7. What is the type of the variable in the following declaration?
    double price = 19.99;
    
    double
    price
    19.99
    Answer

    double

  8. What is the initial value of the variable in the following declaration?
    boolean isActive = true;
    
    boolean
    isActive
    true
    there is no initial value
    Answer

    true

  9. What is the value of result after the following sequence of statements?
    int result = 5;
    result = result + 10;
    result = result - 3;
    
    5
    12
    15
    17
    Answer

    12

  10. Identify the mistakes in the following variable declarations:
    var y = "Hello";
    int y = 10;
    float pi = 3.14
    String name = 'John';
    age = 30;
    int number;
    double area = 45.0;
    Answer
    var y = "Hello"; and int y = 10; // have the same variable name y.
    float pi = 3.14 // is missing a semicolon at the end.
    String name = 'John'; // should use double quotes for the string value: String name = "John";.
    age = 30; // is missing the type declaration.
    int number; // is declared but not initialized.
    
  11. Which of the following are valid variable names in Java?
    name
    _age
    float
    2ndPlace
    $amount
    public
    accountBalance
    Answer

    name
    _age
    $amount
    accountBalance

  12. Choose the best variable name to hold the radius of a circle:
    r
    rad
    circleRadius
    radius
    R
    Answer

    radius

  13. What is the return type of the getArea() method of the Circle class? If unsure, check its method definition.
    int
    double
    float
    void
    Answer

    double

  14. Assume Circle object is created with x, y, and raius. What does the following sequence of statements print?

    Circle c = new Circle(10, 20, 15);
    c.moveHorizontal(30);
    System.out.println(c.getX());
    
    10
    30
    40
    50
    Answer

    40

  15. What does this program print?
    public class Demo
    {
       public static void main(String[] args)
       {
          System.out.print("Hello");
          System.out.print(" ");
          System.out.print("World");
          System.out.println("!");
       }
    }
    
    Hello World!
    HelloWorld!
    Hello World !
    HelloWorld !
    Answer

    Hello World!

  16. Creating a Square Using Rectangle
    Rectangle square = new Rectangle(0, 0, 50, 50);
    square.setColor(Color.BLUE);
    square.fill();
    square.translate(10, 10);
    

    Where will the square be positioned after executing this code?

    At (0, 0)
    At (10, 10)
    At (50, 50)
    At (60, 60)
    Answer

    At (10, 10)

  17. Calling Methods on Different Objects
    Rectangle rect = new Rectangle(10, 20, 30, 40);
    Circle circ = new Circle(15, 25, 20);
    rect.translate(5, 5);
    circ.setColor(Color.GREEN);
    

    What are the new properties of rect and circ after executing the code?

    Rectangle at (10, 20) with no color, Circle at (15, 25) with no color
    Rectangle at (15, 25) with no color, Circle at (15, 25) with no color
    Rectangle at (10, 20) with no color, Circle at (15, 25) with color GREEN
    Rectangle at (15, 25) with no color, Circle at (15, 25) with color GREEN
    Answer

    Rectangle at (15, 25) with no color, Circle at (15, 25) with color GREEN

  18. Result of Executing Methods
    Rectangle rect = new Rectangle(0, 0, 100, 100);
    rect.setColor(Color.RED);
    rect.translate(50, 50);
    rect.grow(20, 20);
    rect.fill();
    

    What will be the final state of the rectangle?

    Rectangle at (0, 0) with width 100, height 100, and color RED
    Rectangle at (50, 50) with width 100, height 100, and color RED
    Rectangle at (50, 50) with width 140, height 140, and color RED
    Rectangle at (70, 70) with width 140, height 140, and color RED
    Answer

    Rectangle at (50, 50) with width 140, height 140, and color RED

  19. Effect of translate Method on Coordinates
    Line line1 = new Line(0, 0, 10, 10);
    line1.translate(5, 5);
    

    What will be the new coordinates of line1?

    (0, 0) to (10, 10)
    (5, 5) to (15, 15)
    (0, 0) to (5, 5)
    (10, 10) to (20, 20)
    Answer

    (5, 5) to (15, 15)

  20. Invoking Multiple Methods on an Object
    Rectangle rect = new Rectangle(10, 10, 20, 20);
    rect.translate(10, 0);
    rect.grow(5, 5);
    

    What will be the new properties of the rectangle?

    Rectangle at (10, 10) with width 20, height 20
    Rectangle at (20, 10) with width 25, height 25
    Rectangle at (20, 10) with width 30, height 30
    Rectangle at (15, 15) with width 25, height 25
    Answer

    Rectangle at (20, 10) with width 30, height 30

  21. Creating and Manipulating a Line Object
    Line line1 = new Line(0, 0, 50, 50);
    line1.setColor(Color.BLUE);
    line1.translate(20, 20);
    line1.draw();
    

    Where will the line be drawn after executing this code?

    From (0, 0) to (50, 50)
    From (20, 20) to (70, 70)
    From (20, 20) to (50, 50)
    From (10, 10) to (60, 60)
    Answer

    From (20, 20) to (70, 70)

  22. Result of Executing setSize and setColor Methods
    Rectangle rect = new Rectangle(10, 20, 30, 40);
    rect.setColor(Color.RED);
    rect.setSize(50, 60);
    rect.fill();
    

    What will be the final properties of the rectangle?

    Rectangle at (10, 20) with width 30, height 40, and color RED
    Rectangle at (10, 20) with width 50, height 60, and color RED
    Rectangle at (10, 20) with width 50, height 60, and no color
    Rectangle at (0, 0) with width 50, height 60, and color RED
    Answer

    Rectangle at (10, 20) with width 50, height 60, and color RED

  23. Creating a Rectangle Object
    Rectangle rect = new Rectangle(10, 20, 30, 40);
    rect.setColor(Color.RED);
    rect.fill();
    

    What will be the result of executing this code?

    A rectangle with no color
    A red rectangle filled with color
    A rectangle with no fill
    A blue rectangle filled with color
    Answer

    A red rectangle filled with color

  24. Using Line and Rectangle Classes
    Line line1 = new Line(10, 10, 50, 10);
    Rectangle rect = new Rectangle(20, 20, 60, 30);
    rect.translate(10, 10);
    line1.translate(5, 0);
    

    After the above code is executed, what are the new coordinates of line1 and rect respectively?

    (15, 10, 55, 10) and (30, 30, 60, 30)
    (15, 10, 50, 10) and (30, 30, 60, 30)
    (15, 10, 55, 10) and (20, 20, 60, 30)
    (10, 10, 50, 10) and (30, 30, 70, 40)
    Answer

    (15, 10, 50, 10) and (30, 30, 60, 30)

  25. Combining Line and Rectangle
    Line line1 = new Line(0, 0, 100, 0);
    Line line2 = new Line(0, 100, 100, 100);
    Rectangle rect = new Rectangle(50, 50, 20, 20);
    line1.translate(0, 50);
    line2.translate(0, -50);
    rect.translate(-30, 30);
    

    Which statement is true after the code execution?

    Both lines will be at y=50 and y=50 respectively.
    The rectangle will be at (20, 80).
    The rectangle will be at (50, 20).
    Both lines will remain at y=0 and y=100 respectively.
    Answer

    The rectangle will be at (20, 80).

  26. Creating a Crate Design Using Line and Rectangle
    Rectangle base = new Rectangle(10, 10, 100, 50);
    Line line1 = new Line(10, 10, 110, 10);
    Line line2 = new Line(10, 60, 110, 60);
    base.setColor(Color.BROWN);
    base.fill();
    line1.setColor(Color.BLACK);
    line2.setColor(Color.BLACK);
    line1.translate(0, 25);
    

    What does the crate look like after executing the code?

    A brown rectangle with a black line in the middle.
    A brown rectangle with a black line at the bottom.
    A brown rectangle with a black line at the top.
    A brown rectangle without any lines.
    Answer

    A brown rectangle with a black line in the middle.

  27. Using Print and Println
    System.out.print("Hello");
    System.out.println("World");
    System.out.println(3 + 4);
    System.out.print("3 + 4");
    

    What will be the output of this code?

    HelloWorld73 + 4
    HelloWorld
    7
    3 + 4
    Hello World 7
    Hello World
    7 3 + 4
    Answer

    HelloWorld
    7
    3 + 4

  28. Using String Methods
    String s = "OpenAI";
    System.out.print(s.charAt(2) + " ");
    System.out.print(s.length() + " ");
    System.out.print(s.substring(1, 4) + " ");
    System.out.print(s.toUpperCase());
    

    What will be the output of this code?

    p 6 pen OpenAI
    e 6 pen OPENAI
    p 5 pen OPENAI
    O 6 pen OPENAI
    Answer

    e 6 pen OPENAI

  29. Combining String Methods
    String s = "Hello World";
    s = s.replace("World", "Java");
    System.out.print(s.substring(0, 5) + " ");
    System.out.println(s.toUpperCase());
    

    What will be the output of this code?

    Hello HELLO JAVA
    Hello HELLO WORLD
    World HELLO JAVA
    Hello HELLO WORLD JAVA
    Answer

    Hello HELLO JAVA

  30. Identify Error Types
    public class Test {
        public static void main(String[] args) {
            int a = 10;
            int b = 0;
            System.out.println(a / b);
            System.out.println("End of program");
        }
    }
    

    What type of error will occur when this code is executed?

    Compile-time error
    Logical error
    Runtime error (ArithmeticException)
    No error, it will print "End of program"
    Answer

    Runtime error (ArithmeticException)

  31. Printing Expressions and Strings
    System.out.print("The result of 2 + 3 is ");
    System.out.println(2 + 3);
    System.out.println("2 + 3");
    

    What will be the output of this code?

    The result of 2 + 3 is 2 + 3
    The result of 2 + 3 is 5
    The result of 2 + 3 is 5
    2 + 3
    The result of 2 + 3 is 5
    5
    Answer

    The result of 2 + 3 is 5
    2 + 3

Back to Top

Slightly harder MCQs

  1. Printing Statements and Combining Strings
    String s1 = "3 + 4";
    int a = 3;
    int b = 4;
    System.out.println(s1);
    System.out.println(a + b);
    System.out.println("Result: " + a + b);
    System.out.println("Result: " + (a + b));
    

    What will be the output of this code?

    3 + 4 7 Result: 34 Result: 34
    3 + 4 7 Result: 7 Result: 7
    3 + 4 7 Result: 34 Result: 7
    3 + 4 34 Result: 34 Result: 7
    Answer

    3 + 4 7 Result: 34 Result: 7

  2. Using Rectangle and Line Classes
    Rectangle rect1 = new Rectangle(20, 20, 40, 40);
    Line line1 = new Line(20, 20, 60, 20);
    rect1.translate(10, 10);
    line1.translate(5, 0);
    

    What are the new coordinates of rect1 and line1 respectively?

    (20, 20, 40, 40) and (25, 20, 65, 20)
    (30, 30, 40, 40) and (25, 20, 60, 20)
    (20, 20, 50, 50) and (25, 20, 65, 20)
    (30, 30, 40, 40) and (20, 20, 60, 20)
    Answer

    (30, 30, 40, 40) and (25, 20, 60, 20)

  3. Creating and Translating a Line Object
    Line line = new Line(0, 0, 100, 0);
    line.translate(50, 50);
    

    What are the new coordinates of the line after translation?

    (0, 0, 100, 50)
    (50, 50, 150, 50)
    (50, 50, 100, 0)
    (0, 0, 50, 50)
    Answer

    (50, 50, 150, 50)

  4. Creating and Translating a Square Using Rectangle
    Rectangle square = new Rectangle(0, 0, 30, 30);
    square.setColor(Color.GREEN);
    square.translate(20, 20);
    square.fill();
    

    What will be the final position and color of the square?

    (0, 0) and GREEN
    (20, 20) and GREEN
    (30, 30) and BLUE
    (20, 20) and RED
    Answer

    (20, 20) and GREEN

  5. Using String Methods and Printing
    String str = "Java Programming";
    System.out.println(str.charAt(5));
    System.out.println(str.substring(0, 4));
    System.out.println(str.replace("Java", "C++"));
    

    What will be the output of this code?

    a Java C++ Programming
    P Java C++ Programming
    P rogramming C++ Programming
    a Java C++ rogramming
    Answer

    P Java C++ Programming

Back to Top