Questions
1 – 15 are worth 4 points each.
1.
If the following Java statements are executed,
what will be displayed?
System.out.print("My
friends are");
System.out.print("Superman\n");
System.out.print("Batman
");
System.out.println("Wonder
Woman");
2.
What is the result of
the following expressions?
3 + 5 * 10 – 1
20 % 7
11 / 2
11.0 / 2.0
3.
What will be the
displayed when the following code is executed?
int
x = 3, y = 6;
y
= y + x;
System.out.println("x
= " + x +
", y = " + y);
4.
What will be displayed
as a result of executing the following code?
int
x = 3;
String
msg = "Welcome!";
String
msg1 = msg.toUpperCase();
String
msg2 = msg.toUpperCase();
char
ltr = msg.charAt(x);
int
strSize = msg.length();
System.out.println(msg1);
System.out.println(msg2);
System.out.println(ltr);
System.out.println(strSize);
5.
What will be the value of ans after the following code has been executed?
int ans = 3;
int x = 10;
int y = 12;
if (x
>= y)
ans = x + y;
6.
What will be the value of bonus after the following code is executed?
int bonus,
sales = 7000;
if (sales
< 5000)
bonus = 200;
else if
(sales < 7500)
bonus = 500;
else if
(sales < 10000)
bonus = 750;
else if
(sales < 20000)
bonus = 1000;
else
bonus = 1250;
7.
What will be printed when the
following code is executed?
double
x = 8736.769;
DecimalFormat
formatter =
new
DecimalFormat("#,###,##0.00");
System.out.println(formatter.format(x));
8.
What will be printed when the
following code is executed?
double
x = 8736.769;
DecimalFormat
formatter =
new DecimalFormat("000000.0000");
System.out.println(formatter.format(x));
9.
What would be the value of discountRate after the following statements are executed?
double
discountRate = 1.0;
int
purchase = 2800;
if
(purchase > 1000)
discountRate = .10;
else
if (purchase > 2000)
discountRate = .20;
else
if (purchase > 3000)
discountRate = .30;
else
discountRate = .40;
10. What would be the value of discountRate after the following statements are executed?
double
discountRate;
char
custType = 'X';
switch
(custType)
{
case 'X':
discountRate = .08;
case 'Y':
discountRate = .06;
case 'Z':
discountRate = .04;
default:
discountRate = 0.0;
}
11. What is the error in the following program? If no error,
write “No error”.
public
class q11
{
public static void main(String[] args)
{
int a = 20, b = 30;
final int c;
c = a + b;
System.out.println(c);
}
}
12. What is the error in the following program? If no error,
write “No error”.
public
class q12
{
public static void main(String[] args)
{
int a = 20, b;
double c = 15.5, d;
b = c * 3;
d = a * 3;
System.out.println(b);
System.out.println(d);
}
}
13. Write a boolean expression to test for:
int x
being a value between 200 and 800, and int y equal to 4.
14. If str1
and str2 are both Strings, write an expression that will correctly determine whether
they are equal.
15. If str1
and str2 are both Strings, write an expression that will correctly determine whether
str1 is less than str2.
16. Convert the following switch statement into an if-else-if
statement (10 points)
switch
(year)
{
case 1:
status = 100;
break;
case 2:
status = 1000;
break;
case 3:
status = 500;
break;
default:
status = 0;
}
17.
Write a complete program that does the following (30 points):
·
A student is asked to enter their major
and the number of boxes of cookies they have sold.
·
The number of points the user has earned
is determined.
o
If a student sells at least 100 boxes,
they get 20 points.
o
If a student sells between 50 and 99
boxes, they get 10 points.
o
If a student sells between 20 and 49
boxes, they get 5 points.
o
If a student sells less than 20 boxes,
they get 0 points.
·
If the major is “Computer Science”, they
get an extra 5 points.
·
The total number of points is output.