wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Programming Quiz

Total questions: 60

Worksheet time: 30mins

Name
Class
Date
1.

What will be the output of the following program? interface I1 { default void m2() { System.out.println("bye"); } void m1(); } @FunctionalInterface interface I2 extends I1 { default void m2() { System.out.println("hello"); } } public class Test { public static void main(String[] args) { I2 x = () -> System.out.println("hi"); x.m1(); x.m2(); } }

a)

hi bye

b)

Compilation Error

c)

hi hello

d)

Runtime Error

2.

What will the output of the following code? public interface A111 { String s = "yo"; public void method1(); } interface B { } interface C extends A111, B { public void method1(); public void method1(int x); }

a)

Compilation succeeds.

b)

Compilation fails due to multiple errors.

c)

Compilation fails due to an error only on line 20.

d)

Compilation fails due to an error only on line 21.

e)

Compilation fails due to an error only on line 22.

3.

What will be the output of the following program? import java.util.function.Predicate; public class Test { public static void main(String[] args) { Predicate greater = a -> a > 15; Predicate less = a -> a < 10; boolean bool = greater.test(20); boolean bool1 = less.test(5); System.out.println(bool); System.out.println(bool1); } }

a)

no output

b)

true true

c)

true false

d)

true

e)

true true

4.

Is there any error in the following code? @FunctionalInterface public interface MyInterface { String myInterface(String a); int hashCode(); String toString(); }

a)

The code will compile.

b)

The code will give a compilation error.

5.

What will be the output of the following program? @FunctionalInterface interface MyInterface { String cal(String value); } public class FunctionalInterfaceExample { public static void main(String[] args) { MyInterface myInterface = (String value) -> 75 + value; System.out.println(myInterface.cal("hi" + 25)); } }

a)

hi7525

b)

hi2527

c)

75hi25

d)

hi100

e)

None of the above

6.

What will be the output of the following program? @FunctionalInterface interface MyInterface { public String nit(String name); } public class FunctionalInterfaceExample { public static void main(String[] args) { MyInterface myInterface = (String name) -> "Welcome to "; System.out.println(myInterface.nit("Learning")); } }

a)

Welcome to Learning

b)

Learning Welcome to

c)

No output

d)

Compile time error

e)

Welcome to

7.

What will be the output of the following program? @FunctionalInterface interface Movie { String movieName(); } public class TestMcq { public static void main(String[] args) { Movie m = () -> return "DDLJ"; System.out.println(m.movieName()); } }

a)

No output

b)

DDLJ

c)

ddlj

d)

Movie

e)

Compile time error

8.

What will be the output of the following program? @FunctionalInterface interface I { int myInterface(int a, int b); } public class TestMcq { public static void main(String[] args) { I i = (a, b) -> { int div = a / b; int addition = a + b; return addition; }; i.myInterface(12, 3); } }

a)

4

b)

15

c)

19

d)

No output

e)

Compile time error

9.

What will be the output of the following program? interface I1 { Object m1(); } interface I2 { String m1(); } class A1 implements I1, I2 { @Override public Object m1() { System.out.println("m1"); return new Object(); } } public class TestMcq { public static void main(String[] args) { A1 a = new A1(); a.m1(); } }

a)

m1

b)

m1 A1@123a3e

c)

Compile time error

d)

Runtime Error

10.

What will be the output of the following program? class Example { int x; int y; public Example(int x, int y) { this.x = x; this.y = y; } } public class TestMcq { public static void main(String[] args) { Predicate p1 = (e) -> e.x < e.y; System.out.println(p1.test(new Example(1, 2))); Predicate p2 = (e) -> e.x > e.y; p2.test(new Example(1, 2)); } }

a)

true false

b)

true

c)

true true

d)

false

e)

No output

11.

Choose the correct option for a sealed class that should not declare a direct superclass?

a)

Final

b)

Non-sealed

c)

Abstract

d)

Sealed

12.

Choose the correct compile-time error for the following code: sealed class Parent permits A { } class A extends Parent { }

a)

The class A with a sealed direct superclass should be declared abstract.

b)

The class A with a sealed direct superclass should be declared either public, private, or protected.

c)

The class A with a sealed direct superclass should be declared default.

d)

The class A with a sealed direct superclass should be declared either final, sealed, or non-sealed.

13.

Which of the following code is true or false? sealed class Parent permits A { } abstract class A extends Parent { }

a)

True

b)

False

14.

What will be the output of the following code? sealed class _10KC permits Course { String course; public _10KC(String course) { this.course = course; } } class Course extends _10KC { int fee; public Course(String course, int fee) { super(course); this.fee = fee; } public int getFee() { return fee; } public String getCourse() { return course; } } public class SealedClassExample { public static void main(String[] args) { Course java = new Course("Java", 3000); System.out.println(java.course + " " + java.fee); Course python = new Course("Python", 4000); System.out.println(python.course + " " + python.fee); } }

a)

Java 3000

b)

Python 4000

c)

Compilation Error

d)

Runtime Error

15.

What will be the output of the following code? sealed class _10KC permits Java, Python { String courseName; public String getCourseName() { return courseName; } } final class Java extends _10KC { int fee; public Java(int fee) { this.fee = fee; } public int getJavaFee() { return fee; } } non-sealed class Python extends _10KC { int fee; public Python(int fee) { this.fee = fee; } public int getPythonFee() { return fee; } } public class SealedClassExample { public static void main(String[] args) { _10KC _10kc = new Java(2000); _10kc.courseName = "Java"; System.out.println(_10kc.getCourseName() + " " + getCourse(_10kc)); _10kc.courseName = "Python"; _10kc = new Python(3000); System.out.println(_10kc.getCourseName() + " " + getCourse(_10kc)); } public static int getCourse(_10KC course) { if (course instanceof Java) { return ((Java) course).getJavaFee(); } else { return ((Python) course).getPythonFee(); } } }

a)

Java 2000

b)

Python 3000

c)

Compilation Error

d)

Runtime Error

16.

What will be the output of the following code? sealed class Parent permits Child1, Child2 { void display() { System.out.println("Parent class"); } } final class Child1 extends Parent { void display() { System.out.println("Child1 class"); } } final class Child2 extends Parent { void display() { System.out.println("Child2 class"); } } public class SealedExample { public static void main(String[] args) { Parent p = new Child1(); p.display(); } }

a)

Parent class

b)

Child1 class

c)

Child2 class

d)

Compilation Error

17.

What will be the output of the following code? sealed class Animal permits Dog, Cat { void sound() { System.out.println("Some sound"); } } final class Dog extends Animal { void sound() { System.out.println("Bark"); } } non-sealed class Cat extends Animal { void sound() { System.out.println("Meow"); } } public class Test { public static void main(String[] args) { Animal a = new Cat(); a.sound(); } }

a)

Some sound

b)

Bark

c)

Meow

d)

Compilation Error

18.

What will be the output of the following code? sealed class Vehicle permits Car, Bike { void type() { System.out.println("Vehicle"); } } final class Car extends Vehicle { void type() { System.out.println("Car"); } } non-sealed class Bike extends Vehicle { void type() { System.out.println("Bike"); } } public class Test { public static void main(String[] args) { Vehicle v = new Car(); v.type(); } }

a)

Vehicle

b)

Car

c)

Bike

d)

Compilation Error

19.

What will be the output of the following code? sealed class Base permits Derived { void show() { System.out.println("Base"); } } non-sealed class Derived extends Base { void show() { System.out.println("Derived"); } } public class Test { public static void main(String[] args) { Base b = new Derived(); b.show(); } }

a)

Base

b)

Derived

c)

Compilation Error

d)

Runtime Error

20.

What will be the output of the following code? sealed class Animal permits Dog { String sound = "Animal sound"; } final class Dog extends Animal { String sound = "Dog sound"; } public class Test { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.sound); } }

a)

Animal sound

b)

Dog sound

c)

Compilation Error

d)

Runtime Error

21.

What will be the output of the following code? sealed class Shape permits Circle, Square { String name = "Shape"; } final class Circle extends Shape { String name = "Circle"; } public class Test { public static void main(String[] args) { Shape s = new Circle(); System.out.println(s.name); } }

a)

Shape

b)

Circle

c)

Compilation Error

d)

Runtime Error

22.

What will be the output of the following code? sealed class Animal permits Mammal, Bird { String type = "Animal"; } final class Mammal extends Animal { String type = "Mammal"; } public class Test { public static void main(String[] args) { Animal a = new Mammal(); System.out.println(a.type); } }

a)

Animal

b)

Mammal

c)

Compilation Error

d)

Runtime Error

23.

What will be the output of the following code? sealed class Parent permits Child1, Child2 { String getName() { return "Parent"; } } final class Child1 extends Parent { String getName() { return "Child1"; } } public class Test { public static void main(String[] args) { Parent p = new Child1(); System.out.println(p.getName()); } }

a)

Parent

b)

Child1

c)

Compilation Error

d)

Runtime Error

24.

What will be the output of the following code? sealed class Animal permits Dog { void sound() { System.out.println("Animal sound"); } } final class Dog extends Animal { void sound() { System.out.println("Dog sound"); } } public class Test { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } }

a)

Animal sound

b)

Dog sound

c)

Compilation Error

d)

Runtime Error

25.

What will be the output of the following code? sealed class Vehicle permits Car { void display() { System.out.println("Vehicle"); } } final class Car extends Vehicle { void display() { System.out.println("Car"); } } public class Test { public static void main(String[] args) { Vehicle v = new Car(); v.display(); } }

a)

Vehicle

b)

Car

c)

Compilation Error

d)

Runtime Error

26.

What will be the output of the following code? sealed class Animal permits Dog { String getName() { return "Animal"; } } final class Dog extends Animal { String getName() { return "Dog"; } } public class Test { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.getName()); } }

a)

Animal

b)

Dog

c)

Compilation Error

d)

Runtime Error

27.

What will be the output of the following code? sealed class Animal permits Cat, Dog { String getSound() { return "Animal sound"; } } final class Cat extends Animal { String getSound() { return "Meow"; } } final class Dog extends Animal { String getSound() { return "Bark"; } } public class Test { public static void main(String[] args) { Animal a = new Cat(); System.out.println(a.getSound()); } }

a)

Animal sound

b)

Meow

c)

Bark

d)

Compilation Error

28.

What will be the output of the following code? sealed class Base permits Derived { String getDescription() { return "Base class"; } } final class Derived extends Base { String getDescription() { return "Derived class"; } } public class Test { public static void main(String[] args) { Base b = new Derived(); System.out.println(b.getDescription()); } }

a)

Base class

b)

Derived class

c)

Compilation Error

d)

Runtime Error

29.

What will be the output of the following code? sealed class Shape permits Circle, Rectangle { String getType() { return "Shape"; } } final class Circle extends Shape { String getType() { return "Circle"; } } public class Test { public static void main(String[] args) { Shape s = new Circle(); System.out.println(s.getType()); } }

a)

Shape

b)

Circle

c)

Compilation Error

d)

Runtime Error

30.

What will be the output of the following code? sealed class Employee permits Manager { String role = "Employee"; } final class Manager extends Employee { String role = "Manager"; } public class Test { public static void main(String[] args) { Employee e = new Manager(); System.out.println(e.role); } }

a)

Employee

b)

Manager

c)

Compilation Error

d)

Runtime Error

31.

Which of the lines is going to raise an error? interface I { public void shoot(); } class B implements I { // line 1 public void shoot() { // some code } } class A extends B implements I {} // line 2

a)

Line 1

b)

Line 2

c)

Code will compile

d)

None of the above

32.

What will be the output of the following code snippet? interface I { Test d(); } interface J { String str = "Java"; } class Test { int i; J j = null; } public class Demo { public static void main(String[] args) { I i = new I() { public Test d() { return new Test(); } }; Test d = i.d(); System.out.println(d.j.str); } }

a)

Null Pointer Exception

b)

"Java"

c)

Compile Time Error

d)

Will print hashcode

33.

What will be the output of the following code? public class Test { public static void main(String[] args) { String a = "Hello"; String b = new String("Hello"); if (a.equals(b)) { System.out.println("True"); } else { System.out.println("False"); } }}

a)

True

b)

False

c)

Compile Error

d)

Runtime Error

34.

What will be the output of the following program? class Test { public static void main(String[] args) { String str = "java"; str.toUpperCase(); System.out.println(str); }}

a)

JAVA

b)

java

c)

Compile Time Error

d)

None of the above

35.

What will be the output of the following code? class Test { public static void main(String[] args) { StringBuilder sb = new StringBuilder("Hello"); sb.append(" World"); System.out.println(sb); }}

a)

Hello

b)

Hello World

c)

Compile Time Error

d)

None of the above

36.

Which of the following methods is used to compare two strings lexicographically?

a)

compareTo()

b)

equals()

c)

compare()

d)

lexicographically()

37.

What will be the result of the following code? class Test { public static void main(String[] args) { String str = "Hello"; System.out.println(str.length()); }}

a)

4

b)

5

c)

Compile Time Error

d)

None of the above

38.

Which of the following is true about the StringBuilder class in Java?

a)

StringBuilder is immutable.

b)

StringBuilder is mutable.

c)

StringBuilder is thread-safe.

d)

StringBuilder is synchronized.

39.

What is the output of the following code? class Test { public static void main(String[] args) { String str = "abc"; str = str.concat("def"); System.out.println(str); }}

a)

abc

b)

abcdef

c)

Compile Time Error

d)

None of the above

40.

What will be the result of the following code? class Test { public static void main(String[] args) { String str = "Java"; str = str.toLowerCase(); System.out.println(str); }}

a)

JAVA

b)

Java

c)

java

d)

None of the above

41.

Which of the following methods of String class is used to extract a substring from a string?

a)

extract()

b)

substring()

c)

cut()

d)

slice()

42.

What will be the output of the following code snippet? class Test { public static void main(String[] args) { String str = "Java Programming"; System.out.println(str.indexOf('P')); }}

a)

5

b)

4

c)

16

d)

15

43.

Which class should be used if you need to create a mutable sequence of characters in Java?

a)

String

b)

StringBuffer

c)

StringBuilder

d)

CharSequence

44.

What is the output of the following code? class Test { public static void main(String[] args) { String str = "Hello"; str = str.replace('l', 'p'); System.out.println(str); }}

a)

Helo

b)

Hepo

c)

Hello

d)

None of the above

45.

Which of the following methods of String class returns the character at a specific index?

a)

charAt()

b)

getChar()

c)

fetchChar()

d)

get()

46.

What will be the output of the following code? class Test { public static void main(String[] args) { String str = "Java"; System.out.println(str.startsWith("Ja")); }}

a)

true

b)

false

c)

Compile Time Error

d)

None of the above

47.

Which method can be used to convert all characters of a string to uppercase?

a)

toUpper()

b)

toUpperCase()

c)

upperCase()

d)

convertToUpper()

48.

What will be the output of the following code? class Test { public static void main(String[] args) { String str = "Java Programming"; System.out.println(str.split(" ")[1]); }}

a)

Java

b)

Programming

c)

Java Programming

d)

Compile Time Error

49.

Which of the following methods is used to check if a string contains a specified sequence of characters?

a)

contains()

b)

has()

c)

include()

d)

exists()

50.

What is the output of the following code snippet? class Test { public static void main(String[] args) { String str = "Welcome"; System.out.println(str.substring(3, 6)); }}

a)

com

b)

come

c)

come

d)

We

51.

Which of the following methods in StringBuilder class is used to reverse the sequence of characters?

a)

reverse()

b)

flip()

c)

invert()

d)

back()

52.

What will be the result of the following code? class Test { public static void main(String[] args) { String str1 = "abc"; String str2 = "def"; System.out.println(str1 + str2); }}

a)

abc

b)

def

c)

abcdef

d)

None of the above

53.

Which of the following methods in the String class is used to check if two strings are equal, ignoring case differences?

a)

equalsIgnoreCase()

b)

compareToIgnoreCase()

c)

isEqual()

d)

compare()

54.

What is the output of the following code? class Test { public static void main(String[] args) { String str = "Java Programming"; System.out.println(str.lastIndexOf('o')); }}

a)

12

b)

16

c)

15

d)

13

55.

Which of the following methods is used to remove leading and trailing spaces from a string?

a)

trim()

b)

strip()

c)

clean()

d)

remove()

56.

What is the output of the following code snippet? class Test { public static void main(String[] args) { String str = "Programming"; System.out.println(str.charAt(5)); }}

a)

o

b)

r

c)

g

d)

m

57.

What will be the result of the following code? class Test { public static void main(String[] args) { String str = "HELLO JAVA"; System.out.println(str.toLowerCase()); }}

a)

hello java

b)

HELLO JAVA

c)

Hello Java

d)

None of the above

58.

Which method of String class is used to replace a specified character with a new character?

a)

replace()

b)

change()

c)

update()

d)

swap()

59.

What will be the output of the following code? class Test { public static void main(String[] args) { String str = "Hello World"; System.out.println(str.contains("world")); }}

a)

true

b)

false

c)

compile time error

d)

runtime error

60.

What is the output of the following code? class Test { public static void main(String[] args) { String str = "abc"; str = str + "def"; System.out.println(str); }}

a)

abc

b)

def

c)

abcdef

d)

None of the above