NEW
Font size
WorksheetsJava Concepts Quiz
Total questions: 51
Worksheet time: 26mins
Which concept hides the internal implementation and only exposes necessary functionality?
Inheritance
Encapsulation
Abstraction
Polymorphism
Which access modifier is used to achieve encapsulation in Java?
public
protected
private
default
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); } }
0
5
Compile-time error
Runtime error
What is abstraction in Java?
Showing all details
Hiding only data
Hiding implementation details
Hiding method names
Which one is not a benefit of encapsulation?
Improved security
Global data access
Code maintainability
Easy testing
Which keyword is used to inherit a class in Java?
implements
inherits
extends
import
Which of these is a has-a relationship?
Inheritance
Aggregation
Overriding
Polymorphism
In Java, which of these can be inherited?
Constructors
Static blocks
Methods
Private variables
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); } }
10
20
30
Compile error
Which statement is true about inheritance?
Subclass inherits private members
Subclass inherits static blocks
Subclass inherits public and protected members
Subclass can inherit multiple classes
What does the super keyword do in Java?
Refers to the current class
Calls subclass constructor
Refers to the immediate parent class
Calls static method
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
B
AB
Compile error
Which of the following allows Java to support runtime polymorphism?
Method Overloading
Inheritance
Method Overriding
Static methods
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()); } }
1
2
Compile error
0
Which operator checks whether an object is of a particular type?
typeOf
belongsTo
instanceof
isType
Which class is the parent of all Java classes?
Class
Object
Main
Super
In which relationship is one class a specialized version of another?
has-a
is-a
uses-a
part-of
Which of these is not a valid form of inheritance in Java?
Single
Multilevel
Multiple
Hierarchical
If class Manager extends Employee, what is true?
Employee is a Manager
Manager is an Employee
Both are same
None of the above
Which keyword is used to call a superclass method in Java?
super
this
parent
extends
What will be the output? String s1 = "Java"; String s2 = "Java"; System.out.println(s1 == s2);
true
false
0
Compile error
How do you create a mutable string in Java?
Using String class
Using StringBuffer class
Using char array
Using final keyword
Which of these methods returns the length of a String?
size()
count()
length()
getSize()
Which method is used to compare two strings for equality, ignoring case?
equals()
compareTo()
equalsIgnoreCase()
toLowerCase()
What will be the output? String s1 = new String("Test"); String s2 = new String("Test"); System.out.println(s1 == s2);
true
false
0
Compile error
What will be the output? String s1 = new String("Test"); String s2 = new String("Test"); System.out.println(s1 == s2);
true
false
Test
Compile error
What is the result of "hello".toUpperCase()?
hello
Hello
HELLO
compile error
Which method adds a string at a specified position in a StringBuffer?
append()
insert()
add()
put()
How do you reverse a StringBuffer?
invert()
reverse()
swap()
flip()
What does String s = "abc"; s = s + "d"; result in?
Changes the original object
Creates a new String object
Modifies existing reference
Causes error
What is the default capacity of a new StringBuffer object?
10
16
32
20
What will System.out.println("Java".charAt(2)); print?
J
v
a
Compile error
Which method can remove a substring from StringBuffer?
remove()
delete()
cut()
erase()
What is the result of System.out.println("hello".compareTo("world"));?
Positive number
Negative number
Zero
Compile error
Which method checks if a string starts with a specific prefix?
starts()
startsWith()
beginsWith()
prefix()
Which will result in a true output? String s1 = "Test"; String s2 = "Test"; System.out.println(s1.equals(s2));
true
false
Compile error
0
What is returned by "abcdef".substring(2,4);?
cd
bc
cde
de
Which of these is a mutable class?
String
StringBuffer
Integer
Character
Which is not allowed as a method override?
Overriding a method with same return type
Overriding a method with a different name
Overriding a method with same signature
Overriding a method with less restrictive access
Which class can be used to build strings efficiently in Java?
String
StringBuffer
StringBuilder
StringTokenizer
Which access specifier allows access only within the same class?
public
private
protected
default
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(); } }
X
Y
XY
Compile error
Which is true about the Object class in Java?
It is the parent of all Java classes
It cannot be extended
It must be inherited
It is an abstract class
Which operator returns false if an object is not an instance of a given class?
is
belongsTo
instanceof
==
What is the use of the finalize() method?
Finalizes a method
Cleans up before garbage collection
Finalizes a class
None of the above
What will be the output of the following code? StringBuffer sb = new StringBuffer("Hello"); sb.append("World"); System.out.println(sb);
Hello
World
HelloWorld
Hello World
Which of these is not a type of association?
Aggregation
Inheritance
Composition
Association
Which method is used to get a part of a StringBuffer as a String?
substring()
getPart()
partString()
slice()
What is output? String s = " Hello "; System.out.println(s.trim());
" Hello "
"Hello"
Hello
Error
What will be the result? String s = "abc"; System.out.println(s.endsWith("c"));
true
false
Compile error
c
What is the result of: System.out.println("Java".replace('a','e'));
Jeve
Jeva
Jeve
Jeve
