wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Concepts Quiz

Total questions: 51

Worksheet time: 26mins

Name
Class
Date
1.

Which concept hides the internal implementation and only exposes necessary functionality?

a)

Inheritance

b)

Encapsulation

c)

Abstraction

d)

Polymorphism

2.

Which access modifier is used to achieve encapsulation in Java?

a)

public

b)

protected

c)

private

d)

default

3.

What will be the output of the following code? class Demo { private int x = 5; public int getX() { return x; } } class Test { public static void main(String[] args) { Demo d = new Demo(); System.out.println(d.x); } }

a)

0

b)

5

c)

Compile-time error

d)

Runtime error

4.

What is abstraction in Java?

a)

Showing all details

b)

Hiding only data

c)

Hiding implementation details

d)

Hiding method names

5.

Which one is not a benefit of encapsulation?

a)

Improved security

b)

Global data access

c)

Code maintainability

d)

Easy testing

6.

Which keyword is used to inherit a class in Java?

a)

implements

b)

inherits

c)

extends

d)

import

7.

Which of these is a has-a relationship?

a)

Inheritance

b)

Aggregation

c)

Overriding

d)

Polymorphism

8.

In Java, which of these can be inherited?

a)

Constructors

b)

Static blocks

c)

Methods

d)

Private variables

9.

What is the result of this code? class A { int x = 10; } class B extends A { int y = 20; } class Test { public static void main(String[] args) { B b = new B(); System.out.println(b.x + b.y); } }

a)

10

b)

20

c)

30

d)

Compile error

10.

Which statement is true about inheritance?

a)

Subclass inherits private members

b)

Subclass inherits static blocks

c)

Subclass inherits public and protected members

d)

Subclass can inherit multiple classes

11.

What does the super keyword do in Java?

a)

Refers to the current class

b)

Calls subclass constructor

c)

Refers to the immediate parent class

d)

Calls static method

12.

What is the output? class A { void show() { System.out.println("A"); } } class B extends A { void show() { System.out.println("B"); } } class Test { public static void main(String[] args) { A obj = new B(); obj.show(); } }

a)

A

b)

B

c)

AB

d)

Compile error

13.

Which of the following allows Java to support runtime polymorphism?

a)

Method Overloading

b)

Inheritance

c)

Method Overriding

d)

Static methods

14.

What is the output of this code? class Parent { int fun() { return 1; } } class Child extends Parent { int fun() { return 2; } } public class Main { public static void main(String[] args) { Parent p = new Child(); System.out.println(p.fun()); } }

a)

1

b)

2

c)

Compile error

d)

0

15.

Which operator checks whether an object is of a particular type?

a)

typeOf

b)

belongsTo

c)

instanceof

d)

isType

16.

Which class is the parent of all Java classes?

a)

Class

b)

Object

c)

Main

d)

Super

17.

In which relationship is one class a specialized version of another?

a)

has-a

b)

is-a

c)

uses-a

d)

part-of

18.

Which of these is not a valid form of inheritance in Java?

a)

Single

b)

Multilevel

c)

Multiple

d)

Hierarchical

19.

If class Manager extends Employee, what is true?

a)

Employee is a Manager

b)

Manager is an Employee

c)

Both are same

d)

None of the above

20.

Which keyword is used to call a superclass method in Java?

a)

super

b)

this

c)

parent

d)

extends

21.

What will be the output? String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2);

a)

true

b)

false

c)

0

d)

Compile error

22.

How do you create a mutable string in Java?

a)

Using String class

b)

Using StringBuffer class

c)

Using char array

d)

Using final keyword

23.

Which of these methods returns the length of a String?

a)

size()

b)

count()

c)

length()

d)

getSize()

24.

Which method is used to compare two strings for equality, ignoring case?

a)

equals()

b)

compareTo()

c)

equalsIgnoreCase()

d)

toLowerCase()

25.

What will be the output? String s1 = new String("Test"); String s2 = new String("Test"); System.out.println(s1 == s2);

a)

true

b)

false

c)

0

d)

Compile error

26.

What will be the output? String s1 = new String("Test"); String s2 = new String("Test"); System.out.println(s1 == s2);

a)

true

b)

false

c)

Test

d)

Compile error

27.

What is the result of "hello".toUpperCase()?

a)

hello

b)

Hello

c)

HELLO

d)

compile error

28.

Which method adds a string at a specified position in a StringBuffer?

a)

append()

b)

insert()

c)

add()

d)

put()

29.

How do you reverse a StringBuffer?

a)

invert()

b)

reverse()

c)

swap()

d)

flip()

30.

What does String s = "abc"; s = s + "d"; result in?

a)

Changes the original object

b)

Creates a new String object

c)

Modifies existing reference

d)

Causes error

31.

What is the default capacity of a new StringBuffer object?

a)

10

b)

16

c)

32

d)

20

32.

What will System.out.println("Java".charAt(2)); print?

a)

J

b)

v

c)

a

d)

Compile error

33.

Which method can remove a substring from StringBuffer?

a)

remove()

b)

delete()

c)

cut()

d)

erase()

34.

What is the result of System.out.println("hello".compareTo("world"));?

a)

Positive number

b)

Negative number

c)

Zero

d)

Compile error

35.

Which method checks if a string starts with a specific prefix?

a)

starts()

b)

startsWith()

c)

beginsWith()

d)

prefix()

36.

Which will result in a true output? String s1 = "Test"; String s2 = "Test"; System.out.println(s1.equals(s2));

a)

true

b)

false

c)

Compile error

d)

0

37.

What is returned by "abcdef".substring(2,4);?

a)

cd

b)

bc

c)

cde

d)

de

38.

Which of these is a mutable class?

a)

String

b)

StringBuffer

c)

Integer

d)

Character

39.

Which is not allowed as a method override?

a)

Overriding a method with same return type

b)

Overriding a method with a different name

c)

Overriding a method with same signature

d)

Overriding a method with less restrictive access

40.

Which class can be used to build strings efficiently in Java?

a)

String

b)

StringBuffer

c)

StringBuilder

d)

StringTokenizer

41.

Which access specifier allows access only within the same class?

a)

public

b)

private

c)

protected

d)

default

42.

What is the output of this code? class X { void print() { System.out.println("X"); } } class Y extends X { void print() { System.out.println("Y"); } } public class Test { public static void main(String[] args) { X obj = new Y(); obj.print(); } }

a)

X

b)

Y

c)

XY

d)

Compile error

43.

Which is true about the Object class in Java?

a)

It is the parent of all Java classes

b)

It cannot be extended

c)

It must be inherited

d)

It is an abstract class

44.

Which operator returns false if an object is not an instance of a given class?

a)

is

b)

belongsTo

c)

instanceof

d)

==

45.

What is the use of the finalize() method?

a)

Finalizes a method

b)

Cleans up before garbage collection

c)

Finalizes a class

d)

None of the above

46.

What will be the output of the following code? StringBuffer sb = new StringBuffer("Hello"); sb.append("World"); System.out.println(sb);

a)

Hello

b)

World

c)

HelloWorld

d)

Hello World

47.

Which of these is not a type of association?

a)

Aggregation

b)

Inheritance

c)

Composition

d)

Association

48.

Which method is used to get a part of a StringBuffer as a String?

a)

substring()

b)

getPart()

c)

partString()

d)

slice()

49.

What is output? String s = " Hello "; System.out.println(s.trim());

a)

" Hello "

b)

"Hello"

c)

Hello

d)

Error

50.

What will be the result? String s = "abc"; System.out.println(s.endsWith("c"));

a)

true

b)

false

c)

Compile error

d)

c

51.

What is the result of: System.out.println("Java".replace('a','e'));

a)

Jeve

b)

Jeva

c)

Jeve

d)

Jeve