Quiz Questions: Java Coding Examples - Prof. Erliang Zeng, Quizzes of Javascript programming

Two java coding quiz questions with explanations for correcting the logic and completing the program. The first question involves finding the maximum of three input integers and the second question tests tire pressure boundary conditions.

Typology: Quizzes

Pre 2010

Uploaded on 03/11/2009

koofers-user-xrq-1
koofers-user-xrq-1 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz 4
Name: Panther ID:
1. [5pts] In the following main method:
public static void main(String[] args)
{
//Input any 3 integers
Scanner input = new Scanner (System.in);
System.out.print("Enter an integer -> ");
int a = input.nextInt();
System.out.print("Enter an integer -> ");
int b = input.nextInt();
System.out.print("Enter an integer -> ");
int c = input.nextInt();
int max;
if (a > b && a > c)
max = a;
else if (b > a && b > c)
max = b;
else
max =c;
System.out.println("Maximum of the input values is: "+
max);
}
Suppose inputs are 20, 30 and 10. Output: ___30___
Suppose inputs are 20, 20 and 5. Output: ____5___
Is the logic correct? If not, please rewrite the conditional statement.
No.
change “>” to “>=” in the if else statements
2. [5pts] In the following main method:
public static void main(String[] args)
{
Scanner input = new Scanner (System.in);
System.out.print("Enter the Tire Pressure (0..75 psi)
-> ");
int pressure = input.nextInt();
System.out.print("Pressure: " + pressure + " psi : ");
String state = "Pressure OK";
//30..35:OK
//under 30 : UNDERinflated
//over 35 : //OVERinflated
if(state < 30)
state = “UNDERinflated”;
else if(state > 35)
state = “OVERinflated”;
}
The above main method is designed to illustrate testing “boundary conditions”.
The program allows the user to enter a tire pressure in psi (pounds per square
inch) and prints “Pressure OK” if the pressure is in the range 30..35, inclusive.
Otherwise, the program should print one of the messages “UNDERinflated” or
“OVERinflated”, whichever is appropriate. Complete the program using
appropriate logic.
pf2

Partial preview of the text

Download Quiz Questions: Java Coding Examples - Prof. Erliang Zeng and more Quizzes Javascript programming in PDF only on Docsity!

Quiz 4

Name: Panther ID:

1. [5pts] In the following main method:

public static void main(String[] args) { //Input any 3 integers Scanner input = new Scanner (System.in); System.out.print("Enter an integer -> "); int a = input.nextInt(); System.out.print("Enter an integer -> "); int b = input.nextInt(); System.out.print("Enter an integer -> "); int c = input.nextInt(); int max; if (a > b && a > c) max = a; else if (b > a && b > c) max = b; else max =c; System.out.println("Maximum of the input values is: "+ max); }

Suppose inputs are 20, 30 and 10. Output: 30

Suppose inputs are 20, 20 and 5. Output: ____5___

Is the logic correct? If not, please rewrite the conditional statement.

No.

change “>” to “>=” in the if else statements

2. [5pts] In the following main method:

public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter the Tire Pressure (0..75 psi) -> "); int pressure = input.nextInt(); System.out.print("Pressure: " + pressure + " psi : "); String state = "Pressure OK"; //30..35:OK //under 30 : UNDERinflated //over 35 : //OVERinflated if(state < 30) state = “UNDERinflated”; else if(state > 35) state = “OVERinflated”; }

The above main method is designed to illustrate testing “boundary conditions”.

The program allows the user to enter a tire pressure in psi (pounds per square

inch) and prints “Pressure OK” if the pressure is in the range 30..35, inclusive.

Otherwise, the program should print one of the messages “UNDERinflated” or

“OVERinflated”, whichever is appropriate. Complete the program using

appropriate logic.

Bonus. [5pts] In the following main method:

public static void main(String[] args) { Scanner input = new Scanner (System.in); System.out.print("Enter a Positive Single Digit (0..9) -> "); int digit = input.nextInt(); int quotient = 0; switch (digit) { case 9 : case 8 : quotient++; case 7 : case 6 : quotient++; case 5 : case 4 : quotient++; case 3 : case 2 : quotient++; default: ; } System.out.println(digit + " / 2 = " + quotient); }

Input Output

What does the program compute?

Division by 2