Code Snippets-based MCQs
Soft Intro to Java with BlueJ (Objects & Classes)
Table of contents
Easy Level MCQs
- Given a
Person
class with a constructor that initializes the name and age of a person and a methodgetName
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());
Answer
Alice
- Given a
Rectangle
class with a constructor that initializes its width and height, and a methodarea
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();
Answer
12
- What will be the output of the following code snippet?
int a = 10; int b = 20; System.out.println(a + b + "" + a + a + "" + b);
Answer
30101020
- Given a
Calculator
class with a methodaddAndPrint
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);
Answer
12
- Given a
Printer
class with a methodprintGreeting
that takes aString
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");
Answer
Hello, Alice!
- What is the variable name in the following declaration?
Car myCar = new Car("Toyota");
Answer
myCar
- What is the type of the variable in the following declaration?
double price = 19.99;
Answer
double
- What is the initial value of the variable in the following declaration?
boolean isActive = true;
Answer
true
- What is the value of
result
after the following sequence of statements?int result = 5; result = result + 10; result = result - 3;
Answer
12
- Identify the mistakes in the following variable declarations:
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.
- Which of the following are valid variable names in Java?
Answer
name
_age
$amount
accountBalance - Choose the best variable name to hold the radius of a circle:
Answer
radius
- What is the return type of the
getArea()
method of theCircle
class? If unsure, check its method definition.Answer
double
-
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());
Answer
40
- 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("!"); } }
Answer
Hello World!
- 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?
Answer
At (10, 10)
- 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
andcirc
after executing the code?Answer
Rectangle at (15, 25) with no color, Circle at (15, 25) with color GREEN
- 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?
Answer
Rectangle at (50, 50) with width 140, height 140, and color RED
- 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?
Answer
(5, 5) to (15, 15)
- 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?
Answer
Rectangle at (20, 10) with width 30, height 30
- 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?
Answer
From (20, 20) to (70, 70)
- 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?
Answer
Rectangle at (10, 20) with width 50, height 60, and color RED
- 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?
Answer
A red rectangle filled with color
- 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?
Answer
(15, 10, 50, 10) and (30, 30, 60, 30)
- 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?
Answer
The rectangle will be at (20, 80).
- 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?
Answer
A brown rectangle with a black line in the middle.
- 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?
Answer
HelloWorld
7
3 + 4 - 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?
Answer
e 6 pen OPENAI
- 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?
Answer
Hello HELLO JAVA
- 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?
Answer
Runtime error (ArithmeticException)
- 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?
Answer
The result of 2 + 3 is 5
2 + 3
Slightly harder MCQs
- 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?
Answer
3 + 4 7 Result: 34 Result: 7
- 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?
Answer
(30, 30, 40, 40) and (25, 20, 60, 20)
- 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?
Answer
(50, 50, 150, 50)
- 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?
Answer
(20, 20) and GREEN
- 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?
Answer
P Java C++ Programming