Sail E0 Webinar

MCQs

Total Questions : 105 | Page 2 of 11 pages
Question 11. What would be the result of attempting to compile and run the following code?
public class HelloWorld{
public static void main(String[] args){
double[] x = new double[]{1, 2, 3};
System.out.println("Value is " + x[1]);
}
}
  1.    The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.
  2.    The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};
  3.    The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};
  4.    The program compiles and runs fine and the output
 Discuss Question
Answer: Option D. -> The program compiles and runs fine and the output
Question 12. Which will legally declare, construct, and initialize an array?
  1.    int [] myList = {};
  2.    int [] myList = (5, 8, 2);
  3.    int myList [] [] = {4,9,7,0};
  4.    int myList [] = {4, 3, 7};
 Discuss Question
Answer: Option D. -> int myList [] = {4, 3, 7};
Question 13. What will be the output of the program?
public class Test{
public static void main(String [] args){
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
System.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is C:Java> java Test 1 2 3 4
  1.    args[2] = 2
  2.    args[2] = 3
  3.    args[2] = null
  4.    An exception is thrown at runtime.
 Discuss Question
Answer: Option D. -> An exception is thrown at runtime.
Question 14. What is the value of a[1] after the following code is executed?
int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
  1.    0
  2.    1
  3.    2
  4.    3
  5.    4
 Discuss Question
Answer: Option B. -> 1
Question 15.


What is the output of this program?


What is the output of this program?
class Output {
public static void main(String args[])
{
int a1[] = new int[10];
int a2[] = {1, 2, 3, 4, 5};
System.out.println(a1.length + " " + a2.length);
}
}
  1.    10 5
  2.    5 10
  3.    0 10
  4.    0 5
 Discuss Question
Answer: Option A. -> 10 5

 Arrays in java are implemented as objects, they contain an attribute that is length which 

contains the number of elements that can be stored in the array. Hence a1.length gives 

10 and a2.length gives 5.

output:
$ javac Output.java
$ java Output
10 5


Question 16.


What is the output of this program?


class Output {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("D");
obj.ensureCapacity(3);
obj.trimToSize();
System.out.println(obj.size());
}
}
  1.    1
  2.    2
  3.    3
  4.    4
 Discuss Question
Answer: Option B. -> 2

trimTosize() is used to reduce the size of the array that underlines an ArrayList object.
Output:
$ javac Output.java
$ java Output
2


Question 17.


What is the output of this program?


import java.util.*;
class Output {
public static void main(String args[]) {
TreeSet t = new TreeSet();
t.add("3");
t.add("9");
t.add("1");
t.add("4");
t.add("8");
System.out.println(t);
}
}
  1.    [1, 3, 5, 8, 9]
  2.    [3, 4, 1, 8, 9]
  3.    [9, 8, 4, 3, 1]
  4.    [1, 3, 4, 8, 9]
 Discuss Question
Answer: Option D. -> [1, 3, 4, 8, 9]

TreeSet class uses set to store the values added by function add in ascending order 

using tree for storage
Output:
$ javac Output.java
$ java Output
[1, 3, 4, 8, 9]


Question 18.


What is the output of this program?


class Output {
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5};
for ( int i = 0; i < arr.length - 2; ++i)
System.out.println(arr[i] + " ");
}
}
  1.    1 2
  2.    1 2 3
  3.    1 2 3 4
  4.    1 2 3 4 5
 Discuss Question
Answer: Option B. -> 1 2 3

arr.length() is 5, so the loop is executed for three times.
output:
$ javac Output.java
$ java Output
1 2 3

Question 19.


What is the output of this program?


class Output {
public static void main(String args[]) {
double x = 2.0;
double y = 3.0;
double z = Math.pow( x, y );
System.out.print(z);
}
}
  1.    2.0
  2.    4.0
  3.    8.0
  4.    9.0
 Discuss Question
Answer: Option C. -> 8.0

Math.pow(x, y) methods returns value of y to the power x, i:e x ^ y, 2.0 ^ 3.0 = 8.0.
Output:
$ javac Output.java
$ java Output
8.0

Question 20.


What is the output of this program?


import java.util.*;
class Maps {
public static void main(String args[]) {
TreeMap obj = new TreeMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj.entrySet());
}
}
  1.    [A, B, C]
  2.    [1, 2, 3]
  3.    {A=1, B=2, C=3}
  4.    [A=1, B=2, C=3]
 Discuss Question
Answer: Option D. -> [A=1, B=2, C=3]

obj.entrySet() method is used to obtain a set that contains the entries in the map. 

This method provides set view of the invoking map.
Output:
$ javac Maps.java
$ java Maps
[A=1, B=2, C=3]


Latest Videos

Latest Test Papers