-
String s1 = new String("TODAY");
String s2 = s1.substring(2, 4);
What is the value of s2?
-
String s3 = new String("TOMORROW");
s3.replace('O', '*');
s3.replace('R', '#');
What is the value of s3?
-
String s4 = new String("TOMORROW");
String s5 = new String("YESTERDAY");
What is the value of s4.compareTo(s5) > 0?
-
What is printed by the following code fragment?
for(int k = 0; k < 2; k = k + 1)
{
for(int j = 3; j > 1; j = j - 1)
{
System.out.print(j);
}
}
-
What is the printed by the following code?
int[] value = {1, 2, 8, 3, -1, -2, 4};
int sum = 0;
for (int k = 1; k < value.length; k += 2)
{
sum = sum + value[k - 1];
}
System.out.println(sum);
-
Consider the following method.
public int isOK(int[] data)
{ int k = 1;
while (k < data.length)
{
if (data[k-1] > data[k])
return k;
k++;
}
return -1;
}
Now consider the following code segment.
int[] array1 = {2, 4, 7, 11, 15, 15};
int hasProperty = isOK(array1);
What is the value of hasProperty?
-
What is printed by the following code fragment?
String s = "Pittsburgh";
int index = 0;
while(s.charAt(index) != 't')
{
index++;
System.out.print(s.charAt(index));
}
-
What is printed by the following code fragment?
String s6 = "Today";
for(int p = 0; p < 5; p++){
if(s6.charAt(p) > 'm')
System.out.print('#');
else
System.out.print('*');
}
-
What is printed by the following code fragment?
int x = 23;
int y = 33;
int z = 10;
if(z >= x || z < y){
if(x != y && x <= z)
System.out.print("HIGH");
else
System.out.print("LOW");
}
else
System.out.print("MIDDLE");
-
Consider the following code segment.
int k = 12345;
int digit = (((k % 1000) - (k % 100))/100);
What is the value of digit?
-
What is printed by the following code segment?
int[] values = {5, 4, 3, 2, 1};
for (int i = 4; i > 0; i--)
values[i - 1] = values[i];
for (int i = 0; i < 5; i++)
System.out.println(values[i]);
-
What is printed by the following code segment?
for (int i = 2; i < 20; i += 3)
if (i % 4 == 1)
System.out.println(i);
-
What is printed by the following code segment?
int[] x = new int[7];
int index = 0;
for (int i = 100; i > 0; i = i / 2)
{
x[index] = i;
index++;
}
for (int i = 0; i < x.length; i++)
System.out.println(x[i]);
-
What is printed by the following code segment?
int[] next = {3, 5, 0, -1, 5, 7, 0, 6};
int i = 1;
while (i >= 0)
{
System.out.println(i);
i = next[i];
}
-
Consider the following method:
public static void mystery(int[] a, int[] b, String s)
{
a = b;
a[0] = a[0] + 1;
s = "a[0]";
}
What is printed by the following code segment?
int[] a = {3};
int[] b = {7};
String s = "hello";
mystery(a, b, s);
System.out.println(a[0]);
System.out.println(b[0]);
System.out.println(s);