COSC 236
Mock Test #1
1.
Identify and correct syntax/logical
errors in each of the following:
A ---------------------------------
int n = 0;
int sum;
while(n <= 10)
sum
= sum + n;
n++;
B ---------------------------------
char grade;
Scanner input = new
Scanner(System.in);
System.out.print("Enter a grade:
");
grade = input.next().charAt(0)
System.out.print("grade = ", grade
+ \n");
C ---------------------------------
int hours, pay-rate;
if(hours <= 40)
System.out.printf("Regular pay at" + pay_rate);
System.out.println("Wages = " + hours * pay_rate)
else;
System.out.println("This guy has overtime, use the
other formula!");
D ---------------------------------
int x = 1;
While(x <= 10);
x++;
}
E ---------------------------------The following code should display the
sum of all numbers 1 - 100:
int i
= 1, sum;
while(i < 100)
sum = sum
+ i;
System.out.print("The sum of the
numbers 1 - 100 = sum + \n);
F --------------------------------- The following code should display the
sum of 2 integers:
int x, y;
Scanner input = new Scanner(System.in);
char again;
do {
System.out.print("Enter 2 integers ");
x =
input.nextInt();
y =
input.nextInt();
System.out.println("The sum = " + x + y);
System.out.println("Do you want to continue? (Y/N):
");
again = input.next.charAt(0); // next read
} while(again = 'Y'
&& again = 'y')
2.
What is the output?
A ---------------------------------
for (int
i = 0; i <= 5; i++) {
for
(int j = 1; j <= i + 1;
j++) {
System.out.print(i);
}
System.out.println();
}
B ---------------------------------
for (int
i = 1; i <= 5; i++) {
for
(int j = 1; j <= 5 - i;
j++) {
System.out.print(" ");
}
System.out.println(i);
}
C ---------------------------------
for (int
i = 1; i <= 5; i++) {
for
(int j = 1; j <= i; j++)
{
System.out.print(" ");
}
System.out.println(i);
}
3. Write
a Java program that calculates how much a person would earn after a period
of time, according to the following rules:
1. The starting salary for the first day is $1.
2. The salary will double each day.
Ask the user to input the number of days worked. The output
should display how much the salary was for each day worked, and show the total
pay at the end of the work period. Get output similar to the sample output
below. Restrictions: don't accept a
number less than 1 for input (the person should work at least one day).
SAMPLE OUTPUT:
How
many days worked? -9
ERROR! Should
be positive. REENTER: 12
Day#
Salary
==================
1 1
2 2
3 4
4 8
5 16
6 32
7 64
8 128
9 256
10 512
11 1024
12
2048
==================
Total pay after 12
days worked: $4095
4. Write
a Java program that reads a 5 digits positive integer and displays the original
number separated into individual digits.
SAMPLE OUTPUT:
Enter
a 5-digit integer: 123456
ERROR! Should have only 5 digits. REENTER: 12345
For number 12345 the
digits are: 1 2 3 4 5