Font size
WorksheetsGlobal Logic (JAVA)
Total questions: 125
Worksheet time: 1hrs 22mins
-Contains no duplicate elements.
-Can contain at most one Null value.
-Elements are not key-value pairs.
-Accessing an element can be almost as fast as performing a similar operation on an array.
Which of these classes provides the specified features?
LinkedList
TreeMap
HashSet
LinkedHashMap
HashMap
Which of the following statement about Set is true?
A HashSet cannot have a null value.
Set allows duplicate values.
Set allows null values but only single occurrence.
Set extends the Java.util.collection interface.
-Entries are organized as key/value pairs.
-Duplicate entries replace old entries.
-Entries are sorted using a Comparator or the Comparable interface.
Which interface of the java.util package offers the specified behavior?
List
Map
Set
SortedSet
SortedMap
{1=C, 2=JavaEE, 4=Python, 3=PHP}
{1=C, 2=JavaSE, 3=Python, 4=PHP}
{1=C, 2=JavaEE, 3=Python, 4=PHP}
{1=C, 2=JavaSE,2=JavaEE, 3=Python, 4=PHP}
NullPointerException
{null=null, a=null, b=JavaSE, c=PHP, d=Python}
{a=null, b=JavaSE, c=PHP, d=Python}
{b=JavaSE, c=PHP, d=Python}
Compilation Fails
-Entries are not organized as key/value pairs.
-Duplicate entries are rejected.
Which interface of the java.util package offers the specified behavior?
List
Map
Set
None of the above
Which of the following is/are correct statements?
HashSet class implements the Set interface
HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.
HashSet permits to have a single null value.
HashMap does not allow duplicate keys however it allows to have duplicate values.
HashMap and HashSet are threadsafe
Which of the following statement(s) is/are TRUE?
Both HashMap and HashSet are not synchronized.
HashMap does not allow duplicate keys however it allows to have duplicate values.
HashMap permits single null key and any number of null values.
Both HashMap and HashSet are synchronized.
Which of the following statement(s) is/are FALSE?
TreeSet does not preserve the insertion order of elements but elements are sorted by keys.
Objects in a TreeSet are stored in a sorted and ascending order.
TreeSet does not allow to insert Heterogeneous objects. It will throw classCastException at Runtime if trying to add hetrogeneous objects.
Comparator is note provided for TreeSet.
Which of the following is/are FALSE about HashMap in java?
It provides the basic implementation of Map interface of Java.
HashMap doesn’t allow duplicate keys but allows duplicate values.
HashMap allows null key also but only once and multiple null values.
Unsynchronized.
Synchronized
List listNames = new ArrayList();
listNames.add("Tom");
listNames.add("Mary");
listNames.add("Peter");
String name2 = (String) listNames.get(1);
System.out.println(name2);
What is the output of the above code
Tom
Mary
Peter
raises Exception
Which of these return type of hasNext() method of an iterator?
Integer
Double
Boolean
Collections Object
What will be the output of the Java program?
{0, 1, 3, 4}
{0, 1, 2, 4}
{0, 1, 2, 3, 4}
{0, 0, 0, 3, 4}
Which of these interface is not part of Java’s collection framework?
List
Sorted List
Set
Sorted Map
What is Collection in Java?
A group of objects
A group of classes
A group of interfaces
None of the mentioned
What is Collection in Java?
A group of objects
A group of classes
A group of interfaces
None of the mentioned
Which of these interface is not part of Java’s collection framework?
List
Sorted List
Set
Sorted Map
What will be the output of the Java program?
{0, 1, 3, 4}
{0, 1, 2, 4}
{0, 1, 2, 3, 4}
{0, 0, 0, 3, 4}
Which of these return type of hasNext() method of an iterator?
Integer
Double
Boolean
Collections Object
Which of these exceptions is thrown by remover() method?
IOException
SystemException
ObjectNotFoundExeception
IllegalStateException
Which of these methods deletes all the elements from invoking collection?
clear();
reset();
delete();
remove();
Which of these interface declares core method that all collections will have?
Collection
EventListner
Comparator
Set
List
Which of these is an incorrect form of using method max() to obtain maximum element?
max(Collection c)
max(Collection c, Comparator comp)
max(Comparator comp)
max(List c)
Which of these methods can convert an object into a List?
SetList()
ConvertList()
CopyList()
singletonList()
Which of these is true about unmodifiableCollection() method?
unmodifiableCollection() returns a collection that cannot be modified.
unmodifiableCollection() method is available only for List and Set.
unmodifiableCollection() is defined in Collection class.
None of the mentioned.
Which of these is static variable defined in Collections?
EMPTY_SET
EMPTY_LIST
EMPTY_MAP
All of the mentioned
What implementation of Iterator can traverse a collection in both directions?
Iterator
ListIterator
SetIterator
MapIterator
Which is faster and uses less memory?
ListEnumeration
Iterator
Enumeration
ListIterator
What will be output of given code
a followed by concurrentModification Exception
a b c
a b
a c
Which interface does java.util.Hashtable implement?
Java.util.Map
Java.util.List
Java.util.HashTable
Java.util.Collection
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order.
Which interface provides that capability?
java.util.Map
java.util.Set
java.util.List
java.util.Collection
You need to store elements in a collection that guarantees that no duplicates are stored and all elements can be accessed in natural order.
Which interface provides that capability?
java.util.Map
java.util.Set
java.util.List
java.util.Collection
Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
java.util.HashSet
java.util.LinkedHashSet
java.util.List
java.util.ArrayList
Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not synchronized?
java.util.HashSet
java.util.LinkedHashSet
java.util.List
java.util.ArrayList
The default capacity of a Vector is:
10
12
8
0
The Comparable interface contains which called?
toCompare
compare
compareTo
compareWith
Find the output
public class TreeMapTest {
public static void main(String args[]) {
Map<Integer, String> m = new TreeMap<Integer, String>();
m.put(11, "audi");
m.put(null, null);
m.put(11, "bmw");
m.put(null, "ferrari");
System.out.println(m.size());
System.out.println(m);
}
Nullpointer exception
compiler error
2
Null value
public class MyClass {
public static void main(String args[]) {
Map<String, String> hashMap = new HashMap<String, String>();
hashMap.put(new String("a"), "audi");
hashMap.put(new String("a"), "ferrari");
System.out.println(hashMap);
}
}
a=audi
a=audi
a=ferrari
a=ferrari
Run time Error
Deque and Queue are derived from
Abstract class
Collection
Abstactcollection
List
What will be output of given code
1 2 3
1 followed by exception
Compile time error
run time error
Which is best suited to a multi-threaded environment?
WeakHashMap
Hashtable
HashMap
ConcurrentHashMap
TreeMap implements?
Dictionary
HashMap
AbstractMap
NavigableMap
Which of these is synchronized?
TreeMap
HashMap
Hashtable
All of the above
TreeMap
doesn't allow null key
allow many null values
Both
allow many null key
How can you sort given HashMap on basis of values
Implement Comparator interface and override its compare method
It is not possible
Implement Comparator interface and override its compareTo method
Implement Comparator interface and override its comparable method
What does Collections.sort internally uses when number of elements are less than 7?
Insertion sort
Merge sort
Quick sort
None
What does Collections.sort internally uses when number of elements are greater than 7?
Insertion sort
Merge sort
Quick sort
None
Which of the following statements are true with respect to sets as objects of Hashset?
Set is always an ordered group of objects.
Set allows duplicate objects.
Set does not allow duplicate objects.
Set supports iterators.
Which of the following statements are correct in relation to collections in Java?
A collection can have objects of different types in it.
The Collection is a class.
A collection can have only one type of object in it.
A collection is same as a standard array.
Which of the following statements are correct with respect to a Map in Java?
A map can have iterators for accessing its elements.
The elements of a map are accessed by the respective keys.
Maps are the same as a SortedSet.
None of these.
Which of the following interfaces are not a part of the Java's Collection Framework?
SortedList
Set
SortedMap
List
Which of the following statements are correct with respect to a List in Java?
A List is implemented in the "first-in, first-out" (FIFO) order.
A List is implemented in the "first-in, last-out" (FILO) order.
Lists support duplicate items.
None of these.
Which of the following interfaces must contain a unique element?
List
Set
Array
Collection
Which of the following method deletes all the elements from invoking (calling) collections?
reset()
clear()
delete()
refresh()
-Contains no duplicate elements.
-Can contain at most one Null value.
-Elements are not key-value pairs.
-Accessing an element can be almost as fast as performing a similar operation on an array.
Which of these classes provides the specified features?
LinkedList
TreeMap
HashSet
LinkedHashMap
HashMap
Which of the following statement about Set is true?
A HashSet cannot have a null value.
Set allows duplicate values.
Set allows null values but only single occurrence.
Set extends the Java.util.collection interface.
-Entries are organized as key/value pairs.
-Duplicate entries replace old entries.
-Entries are sorted using a Comparator or the Comparable interface.
Which interface of the java.util package offers the specified behavior?
List
Map
Set
SortedSet
SortedMap
{1=C, 2=JavaEE, 4=Python, 3=PHP}
{1=C, 2=JavaSE, 3=Python, 4=PHP}
{1=C, 2=JavaEE, 3=Python, 4=PHP}
{1=C, 2=JavaSE,2=JavaEE, 3=Python, 4=PHP}
NullPointerException
{null=null, a=null, b=JavaSE, c=PHP, d=Python}
{a=null, b=JavaSE, c=PHP, d=Python}
{b=JavaSE, c=PHP, d=Python}
Compilation Fails
4
5
6
12
-Entries are not organized as key/value pairs.
-Duplicate entries are rejected.
Which interface of the java.util package offers the specified behavior?
List
Map
Set
None of the above
[Computer, Network, Networks, Programming, Security]
[Computer, Programming, Network, Networks, Programming, Security]
[Computer, Programming, Computer, Networks, Network, Security]
[Computer, Networks, Network, Programming, Security]
List listNames = new ArrayList();
listNames.add("Tom");
listNames.add("Mary");
listNames.add("Peter");
String name2 = (String) listNames.get(1);
System.out.println(name2);
What is the output of the above code
Tom
Mary
Peter
raises Exception
What is Collection in Java?
A group of objects
A group of classes
A group of interfaces
None of the mentioned
Which of these interface is not part of Java’s collection framework?
List
Sorted List
Set
Sorted Map
What will be the output of the Java program?
{0, 1, 3, 4}
{0, 1, 2, 4}
{0, 1, 2, 3, 4}
{0, 0, 0, 3, 4}
Which of these return type of hasNext() method of an iterator?
Integer
Double
Boolean
Collections Object
Which of these methods deletes all the elements from invoking collection?
clear();
reset();
delete();
remove();
How many columns does a have if it is created as follows
int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};?
2
4
8
16
What is the index variable for the element at the first row and first column in array
a?
.
a[0][0]
a[1][1]
a[0][1]
a[1][0]
Which of the following statements assigns the letter S to the third row and first column of a two-dimensional array named strGrid (assuming row-major order).
strGrid[0][2] = "S";
strGrid[1][3] = "S";
strGrid[3][1] = "S";
strGrid[2][0] = "S";
How would you get the value 6 out of the following array
int[][] a = { {2, 4, 6, 8}, {1, 2, 3, 4}};?
a[0][3]
a[1][3]
a[0][2]
a[2][0]
Given the following code segment, what is the value of sum after this code executes?
int[][] matrix = { {1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}};
int sum = 0; int col = matrix[0].length - 2; for (int row = 0; row < 4; row++) { sum = sum + matrix[row][col]; }
4
8
9
12
What are the contents of mat after the following code segment has been executed?
int [][] mat = new int [4][3]; for (int row = 0; row < mat.length; row++) { for (int col = 0; col < mat[0].length; col++) { if (row < col) mat[row][col] = 1; else if (row == col) mat[row][col] = 2; else mat[row][col] = 3; } }
{ {2 3 3}, {1 2 3}, {1 1 2}, {1 1 1}}
{ {2 1 1}, {3 2 1}, {3 3 2}, {3 3 3}}
{ {2 1 1 1}, {3 2 1 1}, {3 3 2 1}}
{ {2 3 3 3}, {1 2 3 3}, {1 1 2 3}}
Given the following code segment, what is the value of sum after this code executes?
int[][] m = { {1,1,1,1},{1,2,3,4},{2,2,2,2},{2,4,6,8}}; int sum = 0; for (int k = 0; k < m.length; k++) { sum = sum + m[m.length-1-k][1]; }
A. 4 B. 6 C. 9 D. 10
4
6
9
10
What are the contents of arr after the following code has been executed?
int[][] arr = { {3,2,1},{1,2,3}}; int value = 0; for (int row = 1; row < arr.length; row++) { for (int col = 1; col < arr[0].length; col++) { if (arr[row][col] % 2 == 1) { arr[row][col] = arr[row][col] + 1; } if (arr[row][col] % 2 == 0) { arr[row][col] = arr[row][col] * 2; } } }
{ {6, 4, 2}, {2, 4, 6}}
{ {3, 4, 2}, {2, 4, 8}}
{ {3, 2, 1}, {1, 4, 6}}
{ {3, 2, 1}, {1, 4, 8}}
Analyze the following code:
public class Test {
public static void main(String[] args) {
boolean[][] x = new boolean[3][];
x[0] = new boolean[1]; x[1] = new boolean[2];
x[2] = new boolean[3];
System.out.println("x[2][2] is " + x[2][2]);
}
}
The program has a compile error because new boolean[3][] is wrong.
. The program has a runtime error because x[2][2] is null.
The program runs and displays x[2][2] is false.
The program runs and displays x[2][2] is null.
Suppose a method p has the following heading:
public static int[][] p()
What return statement may be used in p()?
return 1;
return new int[][]{{1, 2, 3}, {2, 4, 5}};
return new int[]{1, 2, 3};
return int[]{1, 2, 3};
Which of the following is FALSE about arrays on Java
A java array is always an object
Length of array can be changed after creation of array
Arrays in Java are always allocated on heap
Predict the output?
public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}
10 20 30 40 50
Compiler Error
10 20 30 40
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
0
0
garbage value
garbage value
Compiler Error
Exception
public class Main {
public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];
int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}
Compiler Error
0
1 2
3 4 5
6 7 8 9
0
0 0
0 0 0
0 0 0 0
9
7 8
4 5 6
0 1 2 3
Which of the following is the correct way of importing an entire package ‘pkg’?
import pkg.
Import pkg.
import pkg.*
Import pkg.*
Which of the following is/are advantages of packages?
Packages avoid name clashes
Classes, even though they are visible outside their package, can have fields visible to packages only
We can have hidden classes that are used by the packages, but not visible outside.
All of the above
Which of these is a mechanism for naming and visibility control of a class and its content?
Object
Packages
Interfaces
None of the above
Which of this access specifies can be used for a class so that its members can be accessed by a different class in the same package?
Public
Protected
No Modifier
All of the mentioned
Arrays in Java are dynamically allocated using the . . . . operator.
create
dynamic
arrayList
new
Which of the following statements correctly declares a two-dimensional integer array?
int Matrix[ ] = new int[5,4];
int Matrix[ ]; Matrix = new int[5,4];
int Matrix[ ][ ] = new int[5][4];
int Matrix[ ][ ] = int[5][4];
Which of the following can be operands of arithmetic operators?
Numeric
Boolean
Both Numeric & Characters
Characters
What will be the output of the following Java program?
class Output {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c); }
}
3 2 4
3 2 3
2 3 4
2 4 4
3 4 4
Which of these can be returned by the operator &?
Integer
Boolean
Integer or Boolean
Character
What will be the output of the following Java program?
class comma_operator {
public static void main(String args[]) {
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum); }
}
5
4
6
14
Which of these can not be used for a variable name in Java?
identifier
identifier & keyword
keyword
none of the mentioned
What will be the output of the following Java code?
class operators {
public static void main(String args[]) {
int x = 8;
System.out.println(++x * 3 + " " + x); }
}
24 8
24 9
27 9
27 8
Which of the following is not an operator in Java?
instanceof
sizeof
>>>=
new
What will be the output of the following Java program?
class jump_statments {
public static void main(String args[]) {
int x = 2;
int y = 0;
for ( ; y < 10; ++y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " "); }
}
}
1 3 5 7
2 4 6 8
1 3 5 7 9
1 2 3 4 5 6 7 8 9
Which of the following loops will execute the body of loop even when condition controlling the loop is initially false?
while
do-while
for
none of the mentioned
What will be the output of the following Java code?
class Relational_operator {
public static void main(String args[]) {
int var1 = 5;
int var2 = 6;
System.out.print(var1 > var2); }
}
5
6
true
false
Which of these is not a bitwise operator?
&
&=
|=
<=
What is the output of relational operators?
Integer
Boolean
Characters
Double
int len = "jump it!".length();
What is the value of len?
8
7
9
6
This code does not compile.
int x = "quarantine".indexOf("a");
What is the value of x?
4
3
2
This code does not compile.
StringIndexOutOfBoundsException
String s = "whatever".substring(1,4);
What is the value of s?
"hate"
"hat"
"whate"
"what"
StringIndexOutOfBoundsException
String s1 = "hello";
String s2 = new String("hello");
if (s1 == s2) System.out.print("Hey");
else System.out.print("Yo");
What is the output?
Hey
Yo
There is no output.
This code will not compile.
int p = "Go Paw Patrol!".indexOf("p");
What is the value of p?
0
4
3
-1
This code will not compile.
String s = "cool cat".substring(6);
What is the value of s?
"cat"
"at"
"cool c"
This code does not compile.
StringIndexOutOfBoundsException
String s1 = "goat";
String s2 = s1.substring(0,s1.length());
boolean z = s1.equals(s2);
What is the value of z?
true
false
This code will not compile.
StringIndexOutOfBoundsException
String s = "good job".substring(3, 9);
What is the value of s?
"d job"
"od job"
"d jo"
"od jo"
StringIndexOutOfBoundsException
Which code segment should go in the blank so that count is equal to the number of times "whoa" occurs in str?
str.length() -2
str.length() -4
str.length() - 3
str.length() - 1
str.length()
What is the output of the following:
String myWord = "FUN";
String anotherWord = myWord.toLowerCase();
System.out.println(myWord);
fun
Fun
FUN
"fun"
What is the output of the following?
String a = "I will pass the exams.";
String b = " I will pass the exams. ";
if (a.equals(b))
System.out.println("fail");
else
System.out.println(b.substring(9,12));
pass
fail
Compilation Error
I will pass the exams.
What is the output of the following?
String a = "Are you ready kids?";
String b = "Aye! Aye! Captain!";
System.out.println(a.concat(b));
Are you ready kids? Aye! Aye! Captain!
Compilation Error
Are you ready kids?Aye! Aye! Captain!
Aye! Aye! Captain!
What is the output of the following code?
String a = "19";
String b = "75";
System.out.println(a+" "+b);
19 75
94
Compilation Error
1975
What method of class String is used to remove white spaces at the beginning and end of String objects?
remove()
trim()
concat()
Substring()
What does the following print?
System.out.println( "And yet, it moves!".charAt(0) );
A
n
!
The statement is incorrect.
Examine the following fragment:
String world = "World!";
StringBuffer buf = new StringBuffer( "Hello" );
buf.append(' ');
buf.append( world );
How many StringBuffer objects are constructed?
0
1
2
3
Which phrase best describes a String object after it has been constructed?
Changeable
Write Only
Read Only
Inaccessible
After a StringBuffer has been constructed, how many characters may be added to it?
No more after it has been constructed.
Only as many as the original capacity.
As many as needed; it will grow in size if necessary.
A new StringBuffer must be constructed each time characters are added.
What is the potential number of characters in a StringBuffer called?
length
size
width
capacity
What is the current number number of characters in a StringBuffer called?
length
size
width
capacity
What is the output of the following code snippet?
public class Demo{
public static void main(String args[]) {
String s = "String is ";
s.concat("Immutable ");
System.out.println(s+"Immutable");
}
}
String is
String is Immutable
String is Immutable Immutable
String is Immutable String is
The result of the following code
public class Demo
{
public static void main(String args[]){
String str1="Welcome"; //Line-1
String str2=new String(str1); //Line-2
if(str1.equals(str2))
System.out.println("Java");
else
System.out.println("Python");
}
}
Python
Java
Error in Line2
Error in Line1
Identify the result of the following code.
public class Demo{
public static void main(String args[]){
String str = "Student";
System.out.println(str.reverse()); // Line-1
}
}
a) Student
a) tnedutS
a) Error in Line-1
a) Compile Time Error
The output of the following code
public class Demo
{
public static void main(String args[]){
String str = "Java is Life";
//single space between words
StringBuffer sb = new StringBuffer(str);
sb.delete(4,8);
System.out.println(sb);
}
}
Java Life
Javas Life
JavaLife
Java is
