CMSC 201 - Final Exam ACTUAL UPDATED QUESTIONS AND CORRECT
ANSWERS
Which of the following statement is incorrect? 4.int j = 200L;
1. byte b = 40;
2. short s = 200;
3. int i = 200;
4. int j = 200L;
What is item after the following loop terminates? ( line item = 3;
numbers are not a feature of a code)
1: int sum = 0;
2: int item = 0;
3: do {
4: item += 1;
5: sum += item;
6: if (sum > 4)
7: break;
8: } while (item < 5);
"AbA".compareToIgnoreCase("abC") returns ____ _. -2
Convert the following for loop into a do-while loop int sum = 0;
int sum = 0; int i = 0;
for (int i = 0; i< 100; i++) do
{ {
sum += i; sum +=i;
} i++;
} while(i < 100);
,According to Java naming convention, which of the 4. MAX_INTEGER
following is a constant?
1. MAXINTEGER
2. MAX-INTEGER
3. max_integer
4. MAX_INTEGER
5. max-integer
(Sort three numbers) import java.util.Scanner;
Write a method with the following header to display public class SortedNumbers
threenumbers in increasing order (one blank space {
between numbers): public static void displaySortedNumbers(double num1, double num2, double
public static void displaySortedNumbers(double num1, num3)
double num2, double num3) {
double t; if (num1 > num2)
Add a main method to the class that prompts for three {
numbers that are sent to the t = num1; num1 = num2; num2 = t;
displaySortedNumbersmethod. }
if (num1 > num3)
Sample output: {
Enter·the·three·numbers:1.1·3.4·2.3↵ ·1.1·2.3·3.4↵ t = num1; num1 = num3; num3 = t;
}
if (num2 > num3)
{
t = num2; num2 = num3; num3 = t;
}
System.out.println(num1 + " " + num2 + " " + num3);
}
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the three numbers: ");
double num1 = s.nextDouble(); double num2 =
s.nextDouble(); double num3 = s.nextDouble();
displaySortedNumbers(num1, num2, num3);
}
}
Use a switch statement to rewrite the following if switch (numOfYears)
statement {
// Find interest rate based on year case 7: annualInterestRate = 7.25;
if (numOfYears == 7) break;
annualInterestRate = 7.25; case 15: annualInterestRate = 8.50;
else if (numOfYears == 15) break;
annualInterestRate = 8.50; case 30: annualInterestRate = 9.0;
else if (numOfYears == 30) break;
annualInterestRate = 9.0; default:
else System.out.println("Wrong number of years");
{ System.exit(0);
System.out.println("Wrong number of years"); }
System.exit(0);
}
, Does the return statement in the following method cause No
compile errors?
public static void main(String[] args)
{
int max = 0;
if(max != 0)
{
System.Out.Println(max);
}
else
return;
Which of the following is the correct expression that ((x < 100) && (x > 1)) || (x < 0)
evaluates to true if the number x is between 1 and 100 or
the number is negative?
1 < x < 100 && x < 0
((x < 100) && (x > 1)) || (x < 0)
((x < 100) && (x > 1)) && (x < 0)
(1 > x > 100) || (x < 0)
Write a main method to let a user enter student scores, import.java.util.Scanner;
and display the max, min, and average of the score. The public class Calcscore
input 0 signifies the end of input. {
public static void main (string[] args)
{
Scanner input = new Scanner(system.in );
System.out.print(" enter student scores (the input ends when you enter 0: ");
double score = input.nextDouble();
double sum = 0.0;
int count = 0;
double max;
double min;
while (score != 0)
{
sum += score;
count ++;
if ( score > max )
max = score;
if (score < min)
min = score;
System.out.print(" enter student scores (the input ends when you enter 0: ");
double score = input.nextDouble();
}
double average = sum / (double) count;
System.out. println(" the average is: " + average);
System.out.println("the maximum score is: " + max);
System.out.println("the minimum score is: " + min);
}
}
ANSWERS
Which of the following statement is incorrect? 4.int j = 200L;
1. byte b = 40;
2. short s = 200;
3. int i = 200;
4. int j = 200L;
What is item after the following loop terminates? ( line item = 3;
numbers are not a feature of a code)
1: int sum = 0;
2: int item = 0;
3: do {
4: item += 1;
5: sum += item;
6: if (sum > 4)
7: break;
8: } while (item < 5);
"AbA".compareToIgnoreCase("abC") returns ____ _. -2
Convert the following for loop into a do-while loop int sum = 0;
int sum = 0; int i = 0;
for (int i = 0; i< 100; i++) do
{ {
sum += i; sum +=i;
} i++;
} while(i < 100);
,According to Java naming convention, which of the 4. MAX_INTEGER
following is a constant?
1. MAXINTEGER
2. MAX-INTEGER
3. max_integer
4. MAX_INTEGER
5. max-integer
(Sort three numbers) import java.util.Scanner;
Write a method with the following header to display public class SortedNumbers
threenumbers in increasing order (one blank space {
between numbers): public static void displaySortedNumbers(double num1, double num2, double
public static void displaySortedNumbers(double num1, num3)
double num2, double num3) {
double t; if (num1 > num2)
Add a main method to the class that prompts for three {
numbers that are sent to the t = num1; num1 = num2; num2 = t;
displaySortedNumbersmethod. }
if (num1 > num3)
Sample output: {
Enter·the·three·numbers:1.1·3.4·2.3↵ ·1.1·2.3·3.4↵ t = num1; num1 = num3; num3 = t;
}
if (num2 > num3)
{
t = num2; num2 = num3; num3 = t;
}
System.out.println(num1 + " " + num2 + " " + num3);
}
public static void main(String [] args)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the three numbers: ");
double num1 = s.nextDouble(); double num2 =
s.nextDouble(); double num3 = s.nextDouble();
displaySortedNumbers(num1, num2, num3);
}
}
Use a switch statement to rewrite the following if switch (numOfYears)
statement {
// Find interest rate based on year case 7: annualInterestRate = 7.25;
if (numOfYears == 7) break;
annualInterestRate = 7.25; case 15: annualInterestRate = 8.50;
else if (numOfYears == 15) break;
annualInterestRate = 8.50; case 30: annualInterestRate = 9.0;
else if (numOfYears == 30) break;
annualInterestRate = 9.0; default:
else System.out.println("Wrong number of years");
{ System.exit(0);
System.out.println("Wrong number of years"); }
System.exit(0);
}
, Does the return statement in the following method cause No
compile errors?
public static void main(String[] args)
{
int max = 0;
if(max != 0)
{
System.Out.Println(max);
}
else
return;
Which of the following is the correct expression that ((x < 100) && (x > 1)) || (x < 0)
evaluates to true if the number x is between 1 and 100 or
the number is negative?
1 < x < 100 && x < 0
((x < 100) && (x > 1)) || (x < 0)
((x < 100) && (x > 1)) && (x < 0)
(1 > x > 100) || (x < 0)
Write a main method to let a user enter student scores, import.java.util.Scanner;
and display the max, min, and average of the score. The public class Calcscore
input 0 signifies the end of input. {
public static void main (string[] args)
{
Scanner input = new Scanner(system.in );
System.out.print(" enter student scores (the input ends when you enter 0: ");
double score = input.nextDouble();
double sum = 0.0;
int count = 0;
double max;
double min;
while (score != 0)
{
sum += score;
count ++;
if ( score > max )
max = score;
if (score < min)
min = score;
System.out.print(" enter student scores (the input ends when you enter 0: ");
double score = input.nextDouble();
}
double average = sum / (double) count;
System.out. println(" the average is: " + average);
System.out.println("the maximum score is: " + max);
System.out.println("the minimum score is: " + min);
}
}