Sample Questions
1. Write the values that will be printed to the console by the statements below. If the
expression is incorrect, write “error”:
double a = 9.0, b = 2.0;
int i = 9, j = 2;
char c = ‘a’;
a. [Link](i % j);
b. [Link](i / j + a / b);
c. [Link](5 < i < 20);
d. [Link]( (char) (c + 5) );
e. [Link]( ((i == 9) || c == ‘b’) && (j >= 2));
2. Read the following programs, write the values that will be printed to the console:
Question a.
public class test1
{
public static void main(String[] args)
{
int a = 1;
int b = 2;
int c = 3;
if(a > 1)
{
if (b == 2 || c == 3)
{
[Link](a + b + c);
}
else
{
[Link](a - b - c);
}
}
else
{
if (b > 1 && c > 2)
{
[Link](a - b + c);
}
else
{
[Link](a + b - c);
}
}
}
}
Question b.
public class test2
{
public static void main(String[] args)
{
int i = 1;
int sum = 0;
while ( i < 10 )
{
i++;
if (i % 2 == 0)
{
sum = sum + i;
}
else
{
continue;
}
}
[Link](sum);
}
}
3. The following program reads three strings, and then prints out the longest one.
Complete the following program:
import [Link];
public class test2
{
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
int i;
String s1, s2, s3;
s1 = [Link]();
s2 = [Link]();
s3 = [Link]();
}
}