Sail E0 Webinar

MCQs

Total Questions : 29 | Page 1 of 3 pages
Question 1.


What is the output of this program?


class A {
int i;
int j;
A() {
i = 1;
j = 2;
}
}
class Output {
public static void main(String args[])
{
A obj1 = new A();
System.out.print(obj1.toString());
}
}
  1.    true
  2.    false
  3.    String associated with obj1
  4.    Compilation Error
 Discuss Question
Answer: Option C. -> String associated with obj1

toString() is method of class Object, since it is superclass of every class, every object has this 

method. toString() returns the string associated with the calling object.

output:
$ javac Output.java
$ java Output
A@1cd2e5f


Question 2.


What is the output of this program?


class output {
public static void main(String args[])
{
String chars[] = {"a", "b", "c", "a", "c"};
for (int i = 0; i < chars.length; ++i)
for (int j = i + 1; j < chars.length; ++j)
if(chars[i].compareTo(chars[j]) == 0)
System.out.print(chars[j]);
}
}
  1.    ab
  2.    bc
  3.    ca
  4.    ac
 Discuss Question
Answer: Option D. -> ac

compareTo() function returns zero when both the strings are equal, it returns a value less than 

zero if the invoking string is less than the other string being compared and value greater than 

zero when invoking string is greater than the string compared to.
output:
$ javac output.java
$ java output
ac


Question 3.


What is the output of this program?


class String_demo {
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
String s1 = "abcd";
int len1 = s1.length();
int len2 = s.length();
System.out.println(len1 + " " + len2);
}
}
  1.    3 0
  2.    0 3
  3.    3 4
  4.    4 3
 Discuss Question
Answer: Option D. -> 4 3

None.
output:
$ javac String_demo.java
$ java String_demo
4 3


Question 4.


What is the output of this program?


class output {
public static void main(String args[])
{
char ch;
ch = "hello".charAt(1);
System.out.println(ch);
}
}
  1.    h
  2.    e
  3.    l
  4.    o
 Discuss Question
Answer: Option B. -> e

"hello" is a String literal, method charAt() returns the character specified at the index position. 

Character at index position 1 is e of hello, hence ch contains e.
output:
$ javac output.java
$ java output
e


Question 5.


What is the output of this program?


class output {
public static void main(String args[])
{ String s = "Hello World";
int i = s.indexOf('o');
int j = s.lastIndexOf('l');
System.out.print(i + " " + j);
}
}
  1.    4 8
  2.    5 9
  3.    4 9
  4.    5 8
 Discuss Question
Answer: Option C. -> 4 9

indexOf() method returns the index of first occurrence of the character where as lastIndexOf() 

returns the index of last occurrence of the character.
output:
$ javac output.java
$ java output
4 9


Question 6.


What is the output of this program?


class String_demo {
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
  1.    ABC
  2.    BCD
  3.    CDA
  4.    ABCD
 Discuss Question
Answer: Option B. -> BCD

ascii is an array of integers which contains ascii codes of Characters A, B, C, D. String(ascii, 1, 3) 

is an constructor which initializes s with Characters corresponding to ascii codes stored in array ascii,

 starting position being given by 1 & ending position by 3, Thus s stores BCD.
output:
$ javac String_demo.java
$ java String_demo
BCD


Question 7.


What is the output of this program?


class output {
public static void main(String args[])
{
String s1 = "Hello World";
String s2 = s1.substring(0 , 4);
System.out.println(s2);
}
}
  1.    Hell
  2.    Hello
  3.    Worl
  4.    World
 Discuss Question
Answer: Option A. -> Hell

substring(0,4) returns the character from 0 th position to 3 rd position.
output:
$ javac output.java
$ java output
Hell


Question 8.


What is the output of this program?


class String_demo {
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
}
}
  1.    a
  2.    b
  3.    c
  4.    abc
 Discuss Question
Answer: Option D. -> abc

String(chars) is a constructor of class string, it initializes string s with the values stored in character 

array chars, therefore s contains "abc".
output:
$ javac String_demo.java
$ java String_demo
abc


Question 9.


What is the output of this program?


class output {
public static void main(String args[])
{
String s1 = "Hello";
String s2 = new String(s1);
String s3 = "HELLO";
System.out.println(s1.equals(s2) + " " + s2.equals(s3));
}
}
  1.    true true
  2.    false false
  3.    true false
  4.    false true
 Discuss Question
Answer: Option C. -> true false

None.
Output:
$ javac output.java
$ java output
true false


Question 10.


What is the output of this program?


class output {
public static void main(String args[])
{
String s1 = "Hello i love java";
String s2 = new String(s1);
System.out.println((s1 == s2) + " " + s1.equals(s2));
}
}
  1.    true true
  2.    false false
  3.    true false
  4.    false true
 Discuss Question
Answer: Option D. -> false true

The == operator compares two object references to see whether they refer to the same instance, 

where as equals() compares the content of the two objects.
Output:
$ javac output.java
$ java output
false true


Latest Videos

Latest Test Papers