Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Test Guide Example questions.

- Where use a switch?


- How use if/else if/else
- Basic form of if and switch

- Where use a for loop?

- Where use a while loop?

- Where use a do-while loop?

- Difference between while and do-while?

- Can I use a variable without creating it?

- Can I use a variable without assignin it a value?

- How I symbolize that a statement is finished?

- Ways for input and output values

Find the errors:

if radius >= 0); { missing ( before radius and semi colon after the )

area = radius * radius * 3.14159 semi colon after the statement

System.out.println(Area of circle of radius " + radius + " is " + area);

else {

system.out.println("Negative input"); system with a capital S

Try to find in this one:

Scanner keyboard = new scanner(System.in);


// Prompt user for input

System.out.print("Please enter your name: );

name = keyboard.nextLie();

System.out.print("Please enter the amount of shares purchased: ");

shares = keybard.nextInt();

// Calculate and display the price of shares purchased

priceOfShares = shares * 24,89;

System.out.println("The amount paid to purchase " + shares + " shares is $" + priceOfShares
+ ".");

// Calculate and display commission paid

commission = priceOfShares * 0.02;

System.out.printn("A 2% commission is paid to the stockbroker");

System.out.println("The commission paid to the stockbroker is $" + commission + ".");

// Calculate and display the name and total amount

total = priceOfShares + commission;

System.out.println("The total amount spent by " + name + " is $" + total + ".");

Show me the output for the inputs 8, 11 and 14:

double value1,value2,value3, average;

Scanner keyboard = new Scanner(System.in);

System.out.print("Enter the value for the first rainfall: ");

value1 = keyboard.nextDouble();

System.out.print("Enter the value for the second rainfall: ");

value2= keyboard.nextDouble();
System.out.print("Enter the value for the third rainfall: ");

value3 = keyboard.nextDouble();

// Calculate the average

average = (value1 + value2 + value3) / 3; (8+11+14)/3 = 11

if (average < 10.0) { 11 is not less than 10, so the if is not


executed

System.out.println(Safe to travel );

System.out.println(Average is " + average); it will print Average is 11

Try it another one for multiple different inputs:

Scanner input = new Scanner(System.in);

//Create statement for input

System.out.print("Enter the shipping weight: ");

//Get user input

double weight = input.nextDouble();

//Create an if statment

if ((0.00<weight)&&(weight<=1.00)) {

System.out.println("The shipping cost for package weighting " + weight + " pounds is 3.50 dollars.");

else if ((weight>1.00)&&(weight<=3.00)) {

System.out.println("The shipping cost for package weighting " + weight + " pounds is 5.50 dollars.");

}
else if ((weight>3.00)&&(weight<=10.00)) {

System.out.println("The shipping cost for package weighting " + weight + " pounds is 8.50 dollars.");

else if ((weight>10.00)&&(weight<=20.00)) {

System.out.println("The shipping cost for package weighting " + weight + " pounds is 10.50
dollars.");

else

System.out.println("The package cannot be shipped.");

Modify the code below so after the dices are shown, it asks for the user if he wants to play another
round and if user says yes, then plays it again. Remember it needs to play the first time and then ask
if he/she wants to play it again.

Random rand = new Random();

// Simulate rolling the dice.

do {

System.out.println("Rolling the dice...");

die1 = rand.nextInt(6) + 1;

die2 = rand.nextInt(6) + 1;

System.out.println("Their values are:");

System.out.println(die1 + " " + die2);

System.out.print("Roll them again (y = yes)? ");

again = keyboard.nextLine();

} while (again == y);


Try it another one by using some of the code from the class and making modifications on your own.

You might also like