0% found this document useful (0 votes)
10 views8 pages

Practice Test - 5

Its java Practice Test

Uploaded by

deeptasermaraj06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views8 pages

Practice Test - 5

Its java Practice Test

Uploaded by

deeptasermaraj06
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. What will be the output of this code?

class Test {
int x = 10;
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t1.x = 20;
System.out.println(t2.x);
}
}

a) 10
b) 20
c) 0
d) Compilation error

2. Which concept is shown here?

class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}

a) Overloading
b) Overriding
c) Encapsulation
d) Abstraction

3. Which constructor will be called?

class Demo {
Demo() { System.out.println("Default"); }
Demo(int x) { System.out.println("Parameterized"); }
public static void main(String[] args) {
Demo d = new Demo(5);
}
}

a) Default
b) Parameterized
c) Compilation error
d) Both

4. What will be printed?

class Parent {
void display() { System.out.println("Parent"); }
}
class Child extends Parent {
void display() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent p = new Child();
p.display();
}
}

a) Parent
b) Child
c) Compilation error
d) Runtime error

5. Which keyword is used to prevent method overriding?

a) abstract
b) static
c) final
d) private
6. What is the output?

String s1 = "Java";
String s2 = new String("Java");
System.out.println(s1 == s2);

a) true
b) false
c) Compilation error
d) Runtime error

7. Which statement about StringBuffer is TRUE?

a) Immutable like String


b) Mutable
c) Cannot store characters
d) Faster than StringBuilder

8. What will happen here?

try {
int a = 5 / 0;
} catch (Exception e) {
System.out.println("Exception");
}

a) ArithmeticException
b) Exception
c) Runtime error
d) No output

9. Which exception is a checked exception?

a) NullPointerException
b) SQLException
c) ArithmeticException
d) ArrayIndexOutOfBoundsException

10. What is the output?

try {
int arr[] = new int[2];
arr[2] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Caught");
}

a) Compilation error
b) Caught
c) Runtime error
d) No output

11. Which method belongs to String class?

a) reverse()
b) substring()
c) append()
d) capacity()

12. What is the default value of a class reference in Java?

a) ""
b) null
c) 0
d) undefined
13. Which method moves the cursor to the next token in StringTokenizer?

a) next()
b) nextToken()
c) hasNext()
d) split()

14. Which collection allows duplicate elements?

a) HashSet
b) ArrayList
c) TreeSet
d) LinkedHashSet

15. Which collection maintains insertion order?

a) HashSet
b) TreeSet
c) LinkedHashSet
d) PriorityQueue

16. Output of this code?

ArrayList<String> list = new ArrayList<>();


list.add("A");
list.add("B");
list.add("A");
System.out.println(list.size());

a) 2
b) 3
c) 1
d) Error
17. Which collection does not allow null values?

a) ArrayList
b) HashSet
c) TreeSet
d) LinkedList

18. What is the output?

Stack<Integer> stack = new Stack<>();


stack.push(10);
stack.push(20);
stack.pop();
System.out.println(stack.peek());

a) 10
b) 20
c) 0
d) Error

19. Which interface does Queue extend?

a) List
b) Collection
c) Set
d) Iterator

20. Which method removes all elements from a collection?

a) clear()
b) remove()
c) delete()
d) erase()
21. Which interface is implemented by ArrayList?

a) Map
b) List
c) Queue
d) Set

22. What will happen here?

HashSet<String> set = new HashSet<>();


set.add("One");
set.add("One");
System.out.println(set.size());

a) 2
b) 1
c) Error
d) Undefined

23. Which collection guarantees natural ordering of elements?

a) HashSet
b) TreeSet
c) LinkedHashSet
d) ArrayList

24. Which method is used to add an element at the beginning of a LinkedList?

a) add()
b) addFirst()
c) insert()
d) push()

25. Output of this code?

Queue<Integer> q = new LinkedList<>();


q.add(1);
q.add(2);
q.remove();
System.out.println(q.peek());

a) 1
b) 2
c) null
d) Error

You might also like