Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

After the following code is executed, what are the values of c and d? Why? Piggy

ID: 3840435 • Letter: A

Question

After the following code is executed, what are the values of c and d? Why? PiggyBank p = new piggyBank(); PiggyBank q = p; p.addNickels (5); q.addNickels (3); double c = getTotal(); double d = q getTotal (); a. c = 5 d = 3 b. c = 0.25 d = 0.15 c. c = 0.25 d = 0.25 d. c = 0.15 d = 0.15 e. = 0.4 d = 0.4 Consider the following code segment. int numberOfCoins = 5; double taxRate = 4; double number = 3.0 Piggy Bank myMoney=new PiggyBank(); taxRate = numberOfCoins; numberOfCoins = number; Which of the following statements is true? a. A compile-time error occurs in line 2 because 4 is an int and not a double. b. A compile time error occurs in line 4 because an object is not being created with new. c. An error occurs in line 6 because numberofCoins and taxRate are not compatible types. d. An error occurs in line 7 because a double cannot be assigned to an int. e. The program will compile without errors. Consider the following code segment. Why? int sum = 200; int n = 0; if (n! = 0)&& (sum/n>90)) else return sum; What is the result when this code is executed? A run-time error occurs when evaluating sum/n. A compile-time error occurs when evaluating sum/n. 0 is returned. 200 is returned. 400 is returned

Explanation / Answer


2) Since a nickel is 5 cents , So 5 Nickel is 25 cents and 3 Nickel is 15 cents

When we get the total we will get : 0.25 + 0.15 = 0.4
(d) p = 0.4 and q = 0.4

Reason : p and q holds the same reference


3) (d) Double cannot be assigned to an int because int is lower data type than Double it means size of int is 4 bytes and size of double is 8 bytes, So hence theer is an error in Line 7


4)(d)
We have a check n!=0 , that becomes false because n=0 . hence else case will be executed
It returns 200