This is first part of the coding interview questions. You may refer the complete list here :

1. What happens when you compile and run the below program?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com . javatutorial . quiz ; import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; public class Quiz38 { public static void main ( String [ ] args ) { List <Integer> list = new ArrayList <Integer> ( ) ; Integer [ ] arr = { 2 , 10 , 3 } ; list = Arrays . asList ( arr ) ; list . set ( 0 , 3 ) ; System . out . println ( list ) ; list . add ( 1 ) ; System . out . println ( list ) ; } }

Output :

[3,10,3], followed by exception

Arrays.asList() returns a fixed-size list backed by the specified array. Therefore, the arraylist can't grow. So, when add() is called, an exception is thrown.

2. What will be output of following program ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package com . topjavatutorial ; import java . util . ArrayList ; import java . util . List ; public class ArrayListDemo { public static void main ( String [ ] args ) { List list = new ArrayList ( ) ; list . add ( 10 ) ; list . add ( 10 ) ; System . out . print ( list . size ( ) ) ; list . remove ( new Integer ( 10 ) ) ; System . out . print ( list . size ( ) ) ; } }

Output:

21

ArralyList can contain duplicate elements. ArrayList remove() method only removes the first occurrence of a matching element.

3. What will be the output of following Java quiz on PriorityQueue ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package com . topjavatutorial ; import java . util . PriorityQueue ; public class PriorityQueueExample { public static void main ( String [ ] args ) { PriorityQueue <String> pQueue = new PriorityQueue <String> ( ) ; pQueue . add ( "Apple" ) ; pQueue . add ( "Nokia" ) ; pQueue . add ( "Samsung" ) ; pQueue . add ( "Apple" ) ; System . out . print ( pQueue . poll ( ) + " " + pQueue . poll ( ) ) ; System . out . print ( " " + pQueue . peek ( ) + " " + pQueue . poll ( ) ) ; } }

Output:

“Apple Apple Nokia Nokia”

PriorityQueue keeps elements sorted and it can have duplicates. add() and offer() methods both offer same functionality. poll() method removes the first element in queue and returns it, while peek() method returns the first element without removing it.

4. What will be the result for below program?

1 2 3 4 5 6 7 8 9 10 11 12 13 package com . topjavatutorial ; public class Student { int rollNumber ; Student ( int n ) { rollNumber = n ; } }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 package com . topjavatutorial ; import java . util . HashSet ; import java . util . Set ; public class HashSetDemo { public static void main ( String [ ] args ) { Set <Student> students = new HashSet <Student> ( ) ; students . add ( new Student ( 1 ) ) ; students . add ( new Student ( 3 ) ) ; students . add ( new Student ( 4 ) ) ; students . add ( new Student ( 1 ) ) ; students . add ( new Student ( 3 ) ) ; System . out . println ( students . size ( ) ) ; } }

Output

5

Since Student doesn't override equals(), there are 5 objects in the HashSet.

5. Predict output of following program :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 package com . topjavatutorial ; public class Employee implements Comparable <Employee> { int id ; String name ; Employee ( int id , String name ) { this . id = id ; this . name = name ; } @Override public int compareTo ( Employee emp ) { return this . name . compareTo ( emp . name ) ; } }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 package com . topjavatutorial ; import java . util . Comparator ; public class EmployeeComparator implements Comparator <Employee> { @Override public int compare ( Employee emp1 , Employee emp2 ) { return emp2 . id - emp1 . id ; } }

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package com . topjavatutorial ; import java . util . TreeSet ; public class TreeSetDemo { public static void main ( String [ ] args ) { TreeSet <Employee> empTreeSet = new TreeSet <Employee> ( new EmployeeComparator ( ) ) ; Employee emp1 = new Employee ( 20 , "Clark" ) ; Employee emp2 = new Employee ( 24 , "Bernie" ) ; Employee emp3 = new Employee ( 3 , "Alex" ) ; empTreeSet . add ( emp1 ) ; empTreeSet . add ( emp2 ) ; empTreeSet . add ( emp3 ) ; for ( Employee emp : empTreeSet ) System . out . print ( emp . name + " " ) ; } }

Output :

Bernie Clark Alex

Comparator takes precedence over Comparable, when both are implemented.

Next

You may also like :

© 2016 – 2018, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post