Multiple Choice Questions 7
Which of the following statements is valid?
a. int i = new int(30);
b. double d[] = new double[30];
c. int i[] = (3, 4, 3, 2);
d. char[] c = new char();
How can you initialize an array of two characters to 'a' and 'b'?
a. char[] charArray = new char[2]; charArray = {'a', 'b'};
b. char[2] charArray = {'a', 'b'};
c. char[] charArray = {'a', 'b'};
d. None of the above
What is the representation of the third element in an array called a?
a. a[2]
b. a(2)
c. a[3]
d. a(3)
What is the printout for the following code?
class Test
{ public static void main(String[] args)
{ int[] x = new int[3];
System.out.println("x[0] is "+x[0]);
}
}
a. The program has a syntax error because the size of the array wasn't specified when declaring the array.
b. The program has a runtime error because the array elements are not initialized.
c. The program runs fine and displays 0.
d. None of the above
In the following code, what is the printout for list2?
class Test
{ public static void main(String[] args)
{ int list1 = {1, 2, 3};
int list2 = {1, 2, 3};
list2 = list1;
list1 = {1, 1, 1};
for (int i=0; i
System.out.print(list2[i]+" ");
}
}
a. 1 2 3
b. 1 1 1
c. None of the above
Suppose s is a string with the value "java". What will be assigned to x if you e
execute the following code?
char x = s.charAt(4);
a. 'a'
b. 'v'
c. Nothing will be assigned to c, because the execution causes the runtime error StingIndexOutofBoundsException.
What is the output of the following code?
class Test
{ public static void main(String[] args)
{ String s1 = new String("Welcome to Java!");
String s2 = s1;
if (s1 == s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
a. s1 is equal to s2
b. s1 is not equal to s2
What is the output of the following code?
class Test
{ public static void main(String[] args)
{ String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");
if (s1 == s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
a. s1 is equal to s2
b. s1 is not equal to s2
What is the output of the following code?
class Test
{ public static void main(String[] args)
{ String s1 = new String("Welcome to Java!");
String s2 = new String("Welcome to Java!");
if (s1.equals(s2))
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
a. s1 is equal to s2
b. s1 is not equal to s2
The statement What is the output of the following code?
class Test
{ public static void main(String[] args)
{ String s1 = new String("Welcome to Java!");
String s2 = s1.toUpperCase();
if (s1 == s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
a. s1 is equal to s2
b. s1 is not equal to s2
What is the output of the following code?
class Test
{ public static void main(String[] args)
{ String s1 = new String("Welcome to Java");
String s2 = s1;
s1 += "and Welcome to HTML";
if (s1 == s2)
System.out.println("s1 is equal to s2");
else
System.out.println("s1 is not equal to s2");
}
}
a. s1 is equal to s2
b. s1 is not equal to s2
Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
a. String s = new String("new string");
b. String s3 = s1 + s2;
c. s1 >= s2
d. int i = s1.length();
Suppose s1 and s2 are two strings. Which of the following statements or expressions are incorrect?
a. String s3 = s1 - s2;
b. boolean b = s1.compareTo(s2);
c. char c = s1[0];
d. char c = s1.charAt(s1.length()-1);
What is the output of the following program?
import java.util.StringTokenizer;
class TestStringTokenizer
{ public static void main(String[] args)
{ //create a string and string tokenizer
String s = "I/am\learning Java.";
StringTokenizer st = new StringTokenizer(s, "/\.");
//retrieve and display tokens
while (st.hasMoreTokens())
System.out.print(st.nextToken()+" ");
}
}
a. I am learning Java.
b. I/am\learning Java.
c. I am learning Java
d. None of the above
How can you get the word "abc" in the main method from the following call? java Test "+" 3 "abc" 2
a. args[0]
b. args[1]
c. args[2]
d. args[3]
Visited
times since 13 March 1999.