MCQs
None.
Output:
$ javac Array.java
$ java Array
3
obj1.and(obj2) returns an BitSet object which contains elements common to both the
object obj1 and obj2 and stores this BitSet in invoking object that is obj1. Hence obj1
contains 3 & 4.
Output:
$ javac Bitset.java
$ java Bitset
{3, 4}
obj.keySet() returns a set containing all the keys used in properties object, here obj
contains keys AB, BC, CD therefore obj.keySet() returns [AB, BC, CD].
Output:
$ javac properties.java
$ java properties
[AB, BC, CD]
push() and pop() are standard functions of the class stack, push() inserts in the stack
and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used
which removes 2 from the stack then again push is used to insert 5 hence stack
contains elements 3 & 5.
Output:
$ javac stack.java
$ java stack
[3, 5]
What is the output of this program?
class static_out {
static int x;
static int y;
void add(int a , int b){
x = a + b;
y = x + b;
}
}
class static_use {
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
None.
output:
$ javac static_use.java
$ java static_use
7 9
Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the
value is stored only at index 0, therefore obj.size() returns the total number of elements
stored in the obj i:e 1, it has nothing to do with ensureCapacity().
Output:
$ javac Output.java
$ java Output
1
obj.get("B") method is used to obtain the value associated with key "B", which is 2.
Output:
$ javac Maps.java
$ java Maps
2
None.
Output:
$ javac Output.java
$ java Output
4.5
HashSet obj creates an hash object which implements Set interface, obj.size() gives the
number of elements stored in the object obj which in this case is 3.
Output:
$ javac Output.java
$ java Output
[A, B, C] 3
array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills
the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1.
Output:
$ javac Array.java
$ java Array
58881