NEW
Font size
WorksheetsJava Programming Quiz
Total questions: 60
Worksheet time: 30mins
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(); } }
hi bye
Compilation Error
hi hello
Runtime Error
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); }
Compilation succeeds.
Compilation fails due to multiple errors.
Compilation fails due to an error only on line 20.
Compilation fails due to an error only on line 21.
Compilation fails due to an error only on line 22.
What will be the output of the following program? import java.util.function.Predicate; public class Test { public static void main(String[] args) { Predicate
no output
true true
true false
true
true true
Is there any error in the following code? @FunctionalInterface public interface MyInterface { String myInterface(String a); int hashCode(); String toString(); }
The code will compile.
The code will give a compilation error.
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)); } }
hi7525
hi2527
75hi25
hi100
None of the above
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")); } }
Welcome to Learning
Learning Welcome to
No output
Compile time error
Welcome to
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()); } }
No output
DDLJ
ddlj
Movie
Compile time error
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); } }
4
15
19
No output
Compile time error
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(); } }
m1
m1 A1@123a3e
Compile time error
Runtime Error
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
true false
true
true true
false
No output
Choose the correct option for a sealed class that should not declare a direct superclass?
Final
Non-sealed
Abstract
Sealed
Choose the correct compile-time error for the following code: sealed class Parent permits A { } class A extends Parent { }
The class A with a sealed direct superclass should be declared abstract.
The class A with a sealed direct superclass should be declared either public, private, or protected.
The class A with a sealed direct superclass should be declared default.
The class A with a sealed direct superclass should be declared either final, sealed, or non-sealed.
Which of the following code is true or false? sealed class Parent permits A { } abstract class A extends Parent { }
True
False
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); } }
Java 3000
Python 4000
Compilation Error
Runtime Error
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(); } } }
Java 2000
Python 3000
Compilation Error
Runtime Error
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(); } }
Parent class
Child1 class
Child2 class
Compilation Error
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(); } }
Some sound
Bark
Meow
Compilation Error
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(); } }
Vehicle
Car
Bike
Compilation Error
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(); } }
Base
Derived
Compilation Error
Runtime Error
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); } }
Animal sound
Dog sound
Compilation Error
Runtime Error
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); } }
Shape
Circle
Compilation Error
Runtime Error
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); } }
Animal
Mammal
Compilation Error
Runtime Error
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()); } }
Parent
Child1
Compilation Error
Runtime Error
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(); } }
Animal sound
Dog sound
Compilation Error
Runtime Error
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(); } }
Vehicle
Car
Compilation Error
Runtime Error
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()); } }
Animal
Dog
Compilation Error
Runtime Error
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()); } }
Animal sound
Meow
Bark
Compilation Error
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()); } }
Base class
Derived class
Compilation Error
Runtime Error
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()); } }
Shape
Circle
Compilation Error
Runtime Error
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); } }
Employee
Manager
Compilation Error
Runtime Error
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
Line 1
Line 2
Code will compile
None of the above
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); } }
Null Pointer Exception
"Java"
Compile Time Error
Will print hashcode
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"); } }}
True
False
Compile Error
Runtime Error
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); }}
JAVA
java
Compile Time Error
None of the above
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); }}
Hello
Hello World
Compile Time Error
None of the above
Which of the following methods is used to compare two strings lexicographically?
compareTo()
equals()
compare()
lexicographically()
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()); }}
4
5
Compile Time Error
None of the above
Which of the following is true about the StringBuilder class in Java?
StringBuilder is immutable.
StringBuilder is mutable.
StringBuilder is thread-safe.
StringBuilder is synchronized.
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); }}
abc
abcdef
Compile Time Error
None of the above
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); }}
JAVA
Java
java
None of the above
Which of the following methods of String class is used to extract a substring from a string?
extract()
substring()
cut()
slice()
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')); }}
5
4
16
15
Which class should be used if you need to create a mutable sequence of characters in Java?
String
StringBuffer
StringBuilder
CharSequence
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); }}
Helo
Hepo
Hello
None of the above
Which of the following methods of String class returns the character at a specific index?
charAt()
getChar()
fetchChar()
get()
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")); }}
true
false
Compile Time Error
None of the above
Which method can be used to convert all characters of a string to uppercase?
toUpper()
toUpperCase()
upperCase()
convertToUpper()
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]); }}
Java
Programming
Java Programming
Compile Time Error
Which of the following methods is used to check if a string contains a specified sequence of characters?
contains()
has()
include()
exists()
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)); }}
com
come
come
We
Which of the following methods in StringBuilder class is used to reverse the sequence of characters?
reverse()
flip()
invert()
back()
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); }}
abc
def
abcdef
None of the above
Which of the following methods in the String class is used to check if two strings are equal, ignoring case differences?
equalsIgnoreCase()
compareToIgnoreCase()
isEqual()
compare()
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')); }}
12
16
15
13
Which of the following methods is used to remove leading and trailing spaces from a string?
trim()
strip()
clean()
remove()
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)); }}
o
r
g
m
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()); }}
hello java
HELLO JAVA
Hello Java
None of the above
Which method of String class is used to replace a specified character with a new character?
replace()
change()
update()
swap()
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")); }}
true
false
compile time error
runtime error
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); }}
abc
def
abcdef
None of the above
