Test Summary
No. of Sections: 1
Total Questions: 15
Total Duration: 40 mins
Section 1 - Code Based Test 3
Section Summary
No. of Questions: 15
Duration: 40 mins
Q1) Fill in the blanks to create a functional interface and use lambda. Output: Sum is: 45
Code Snippet
interface Adder {
int __________(int a, int b);
}
public class Main {
public static void main(String[] args) {
Adder add = (x, y) -> x + __________;
System.out.println("Sum is: " + add.add(20, 25));
}
}
Q2) Fill in the blanks to check if the sum of two variables is divisible by 7 and 5. Output: Divisible
by 7 and 5
Code Snippet
public class Main {
public static void main(String[] args) {
int a = 35, b = 70, sum = a + b;
if(__________________ && ________________) {
System.out.println("Divisible by 7 and 5");
}
}
}
Q3) Fill in the blanks to overload method that prints employee details. Output: ID: 1001, Name:
Meena ID: 1002, Name: Raj, Department: Sales
Code Snippet
class Employee {
void display(int id, String name) {
System.out.println("ID: " + id + ", Name: " + name);
}
void display(int id, String name, String dept) {
__________
System.out.println("Department: " + __________);
}
public static void main(String[] args) {
Employee e = new Employee();
e.display(1001, "Meena");
e.display(1002, "Raj", "Sales");
}
}
Q4) Fill in the blanks where local class prints its method with enclosing method’s name. Output:
Inside display(), local method called
Code Snippet
public class Scope {
void display() {
class Inner {
void localMethod() {
System.out.println("Inside display(), local method __________");
}
}
Inner i = new Inner();
i.__________();
}
public static void main(String[] args) {
new Scope().display();
}
}
Q5) Replace the error to print the current month using Calendar. Expected Output: (0-based index for
month, e.g., 3 for April)
Code Snippet
import java.util.Calendar;
class CalTest {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println(cal.MONTH); // Error
}
}
Options
1) cal.get(Calendar.MONTH)
2) cal.MONTH()
3) cal.getMonth()
4) Calendar.getMonth()
Q6) Identify the correct replacement for the marked issue so that the program prints "20."
Code Snippet
public class Cart {
public static void main(String[] args) {
int result = (10 - 2 + 4) * 3; // Issue
System.out.println(result);
}
}
Options
1) int result = 10 - 2 * 4 * 3;
2) int result = 10 - (2 - 4) * 3;
3) int result = (10 - 2 * 4) /3;
4) int result = 10 - (2 - 4 * 3);
Q7) Identify the correct replacement for the marked issue so the program prints only "Affordable".
Code Snippet
public class Cart {
public static void main(String[] args) {
int price = 150;
if (price > 100) // Issue
System.out.println("Expensive");
System.out.println("Affordable");
}
}
Options
1) if (price = 100)
2) if (price == 100)
3) if (price >= 100)
4) if (price < 100)
Q8) Identify the correct replacement for the marked issue so that the program prints "Bark"
Code Snippet
interface Animal {
default void sound() {
System.out.println("Some sound");
}
}
class Dog implements Animal {
void sound() { // Issue
System.out.println("Bark");
}
}
class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Options
1) public void sound() {
2) private void sound() {
3) protected void sound() {
4) default void sound() {
Q9) Arrange the code where a subclass calls its parent class constructor using super.
Code Snippet
1. System.out.println("Animal Constructor");
2. System.out.println("Dog Constructor");
3. Dog() {
4. class Dog extends Animal {
5. public class Main {
6. public static void main(String[] args) {
7. Dog d = new Dog();
8. class Animal {
9. Animal() {
}
}
}
}
}
}
Options
1) 8, 9, 1, 4, 3, 2, 5, 6, 7
2) 8, 1, 9, 4, 2, 3, 5, 6, 7
3) 4, 3, 2, 8, 9, 1, 5, 6, 7
4) 8, 9, 4, 3, 1, 2, 5, 6, 7
Q10) Arrange the code to override a method in child class and call it through parent reference.
Code Snippet
1. Parent ref = new Child();
2. void display() {
3. class Child extends Parent {
4. System.out.println("Child Display");
5. public static void main(String[] args) {
6. ref.display();
7. System.out.println("Parent Display");
8. class Parent {
9. public class Main {
}
}
}
}
}
}
Options
1) 8, 2, 7, 3, 2, 4, 9, 5, 1, 6
2) 3, 2, 4, 8, 2, 7, 9, 5, 1, 6
3) 8, 2, 3, 4, 9, 1, 5, 6, 7
4) 9, 5, 1, 6, 8, 2, 7, 3, 2, 4
Q11) Arrange the following lines of code to determine if this year is a leap year
Code Snippet
import java.time.*;
public class LeapYearChecker {
1. LocalDate today = LocalDate.now();
2. public static void main(String[] args) {
3. System.out.println("Current year: " + year);
4. System.out.println("Is " + year + " a leap year? " + isLeapYear);
5. int year = today.getYear();
6. boolean isLeapYear = today.isLeapYear();
}
}
Options
1) 2, 1, 3, 5, 6, 4
2) 2, 1, 5, 3, 6, 4
3) 2, 1, 6, 5, 3, 4
4) 2, 1, 5, 6, 3, 4
Q12) Arrange the code to get the output
import java.util.Scanner;
class ReadAndPrint {
public static void main(String[] args) {
1. System.out.println("You entered: " + input);
2. Scanner sc = new Scanner(System.in);
3. String input = sc.nextLine();
4. sc.close();
}
}
Options
1) 3 2 4 1
2) 2 3 1 4
3) 3 2 1 4
4) 2 1 3 4
Q13) Which of the following statements correctly demonstrates method overloading?
A.
class Calculator {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
B.
class Calculator {
int add(int a, int b) {
return a + b;
}
String add(int a, int b) {
return String.valueOf(a + b);
}
}
C.
class Calculator {
void add(int a, int b) {
System.out.println(a + b);
}
void add(int a, int b, int c) {
System.out.println(a + b + c);
}
}
D.
class Calculator {
void add(double a, double b) {
System.out.println(a + b);
}
}
Options
1) A, C
2) B, C
3) A, D
4) C, D
Q14) Which of the following demonstrate correct inheritance in Java?
A.
class Animal {
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
public class Test {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
B.
class Vehicle {
void drive() {
System.out.println("Driving");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car started");
}
}
public class Test {
public static void main(String[] args) {
Car car = new Car();
car.drive();
car.start();
}
}
C.
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class Employee extends Person {
double salary;
Employee(String name, double salary) {
super(name);
this.salary = salary;
}
}
public class Test {
public static void main(String[] args) {
Employee emp = new Employee("John", 50000);
System.out.println(emp.name + " earns " + emp.salary);
}
}
D.
class Animal {
void sleep() {
System.out.println("Sleeping");
}
}
class Bird extends Animal {
void sleep() {
System.out.println("Bird sleeping");
}
}
public class Test {
public static void main(String[] args) {
Bird bird = new Bird();
bird.sleep();
}
}
Options
1) A, B, C
2) A, D
3) B, C, D
4) A, B, C,D
Q15) Which of the following Java programs will compile and output 15?
A.
public class Test {
public static void main(String[] args) {
int a = 10 + 5;
System.out.println(a);
}
}
B.
public class Test {
public static void main(String[] args) {
int a = 30 / 0;
System.out.println(a);
}
}
C.
public class Test {
public static void main(String[] args) {
int a = 20 - 5;
System.out.println(a);
}
}
D.
public class Test {
public static void main(String[] args) {
int a = 3 * 5;
System.out.println(a);
}
}
Options
1) A, B, C
2) B, C, D
3) A, C, D
4) A, B, D