Question: Question 1: Operator Trace A What is the value of num after each statement in the code below? int num = 4; num += 3;

Question 1: Operator Trace A

What is the value of num after each statement in the code below?

int num = 4; num += 3; num /= 3; num = num * (3 + num);

What is the value of d after each statement in the code below?

int n = 3; double d = Math.pow(n, 2); Math.pow(d, 3);

What is the value of dub after each statement in the code below?

double dub = 3; dub *= 4; dub = dub / 5; dub -= 7;

Question 2: Operator Trace B

What are the values of the expressions below?

int a=2, b=3, c=5, d=6; double x = 3.0, y = 4.0; ( (d / c) + (c - d) ) * (a + b) (c / b) + (c / y) (x + y) / (c / d) (y / x) + (b / a)

Question 3: Modulous

What are the values of the expressions below?

12 % 7 15 % 5 6 % 13 3 % 9 -14 % 5 0 % 6

Question 4: Distance Conversion A

Write a complete method to determine and print how many inches, feet, yards, and miles are represented by a total number of inches. You can use the conversion data below.

For example, if numInches = 99,999, then your program would print that this is equivalent to 1 mile, 1,017 yards, 2 feet, and 3 inches.

1 foot = 12 inches

1 yard = 3 feet = 36 inches

1 mile = 1,760 yards = 5,280 feet = 63,360 inches

Question 5: Distance Conversion B

Write a complete method to determine and return the total number of inches represented by a number of miles, yards, feet, and inches. (This is essentially the reverse of Question 4.) Use the conversion data from Question 4.

Question 6: Random Numbers

Write a Java statement to generate a random number in the following range:

-100 to 100 (inclusive)

even numbers from 1 to 100

a four-digit number (a number between 1000 and 9999)

odd numbers between -10 and 10

What is the range of possible random numbers generated by these statements:

int num = generator.nextInt(33) - 7; int num = generator.nextInt(50) + 5;

Question 7: Random Numbers Method

Write a complete method that takes in two numbers: a lower bound and upper bound. Generate and return a random number between these bounds, inclusive.

Question 8: Pizza Method

Write a method to print out the amount of pizza each person will get. The method header is:

public void printPizzaPerPerson(int numberOfPizzas, int numberOfPeople)

For example, if invoked with numberOfPizzas=2 and numberOfPeople = 4, it would print something like "Each person gets 0.5 pizzas."

Question 9: Evaluating Boolean Expressions

Is each of the following a valid Java boolean expression? If so, what is the result of the expression when x=1?

a. (true) && (3>4)

b. !(x<0) && (x>-2)

c. (x>0) || (x<0)

d. (x!=0) && (x>=1)

e. (x<=2) || (x%2==0)

f. (0 < x < 5)

Question 10: Tracing Conditionals

What is the output of the following when x=3 and y=2? What if x=3 and y=4? x=2 and y=2?

if(x > 2) { if(y > 2) { System.out.println(x+y); } } else { System.out.println(x); } if(x > 1) { System.out.println("a"); } else if(x > 2) { System.out.println("b"); }

Question 11: Tracing Switch

What is the value of x after the following switch? What if the initial value is x = 1? x = 2? x= 6?

int x = 4; switch(x) { case 2: x += 3; case 3: x *= 2; break; case 4: x /= 1; case 5: x %= 2; case 6: x = 7; break; case 7: x = 10; default: x = -1; }

Question 12: Evaluating Conditionals

What, if anything, is wrong with the following code?

if(score >= 60) { grade = 'D'; } else if(score >= 70) { grade = 'C'; } else if(score >= 80) { grade = 'B'; } else if(score >= 90) { grade = 'A'; } else { grade = 'F'; }

Will the three conditionals below all print the correct result? If so, which structure is best and why?

Conditional A:

if(age < 16) { System.out.println("you cannot drive"); } else { System.out.println("you can drive!"); }

Conditional B:

if(age < 16) { System.out.println("you cannot drive"); } else if(age >= 16) { System.out.println("you can drive!"); }

Conditional C:

if(age < 16) { System.out.println("you cannot drive"); } if(age >= 16) { System.out.println("you can drive!"); }

Question 13: Writing Switch

Write a complete method to return the number of days in the month based on the month number (sent in as a parameter). Use a switch statement.

Question 14: Writing Conditionals A

Write a complete method that determines if the user is eligible for a loan based on the rules below. Return the status of whether the user is approved.

  • Income below 50,000: must have a co-signer to be eligible
  • Income between 50,000 and 100,000: if the user has been at his job for less than 2 years, he/she must have a co-signer; otherwise, he/she is approved
  • Income above 100,000 is approved

Question 15: Writing Conditionals B- Leap Year

Write a complete method that determines if a year is a leap year. The method takes the year as a parameter. The leap year rules are below.

  • A leap year is a year whose number is perfectly divisible by 4.
    • Except: If a year is divisible by 4, divisible by 100, and NOT divisible by 400 , it is not a leap year.
  • Another way to state this is: A leap year is a year if either:
    • it is divisible by 4 but not by 100 or
    • it is divisible by 400
  • The rules are only complicated for century years.
  • Example: the century years 1600 and 2000 are leap years: divisible by 4, divisible by 100, and also divisible by 400 (or, using the second definition, divisible by 400)
  • Example: the century years 1700, 1800, and 1900 are not leap years: divisible by 4, divisible by 100, but NOT divisible by 400 (or using the second definition, they fail the first test because divisible by 100 and they fail the second test because not divisible by 400)

Question 16: Rewriting Conditionals

Rewrite the following code into a single statement that does not use the conditional (ternary) operator.

boolean even; if(number%2 == 0 ){ even = true; } else { even = false; }

Rewrite the following code into a single statement that uses the conditional (ternary) operator.

int answer; if(x < y) { answer = x + y; } else { answer = x - y; }

Question 17: Tracing while Loop

What is the value of x after the following loop?

int x = 1, y = 0; while(y < 10) { x += y; y += 2; }

Question 18: Tracing do-while Loop

What is the value of x after the following loop?

int x = 1; do { x += 2; } while(x < 20);

Question 19: Tracing for Loop

What is the value of x after the following loop?

int x = 1; for(int i=5; i>0; i--) { x *= i; }

Question 20: Tracing Nested Loops

What is the output of the following?

for(int i=3; i>=0; i--) { for(int j=i; j>=0; j--) { System.out.print(i+""+j); } System.out.println(); }

Question 21: Translating Loops

Translate the while-loop to a for-loop and the for-loop to a while-loop that will have the same output.

int n = 4; while(n < 100) { System.out.println(n%2==0); n+=3; }

for(int i=0; i<=100; i++) { System.out.println(i); } 

Question 22: Writing Loops

Write a complete method that calculates the sum of all integers between two values. For example, the sum of integers between 1 and 10 is 55.

Question 23: Writing Nested Loops

Write a complete method to input the size of a square and then display a square of Xs, such as:

XXX XXXX

XXX or XXXX

XXX XXXX

XXXX

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!