Practice Problems Sol
Practice Problems Sol
Practice Problems Sol
3* 2 + 4 * 3 ________18__________
17 % 9 - 20 % (26 / 7) ________6___________
2 + 5 + "2" + 2 + 5 _________”7225”_____
3 * (2 + 4) * 2 / 10 ________3___________
a) a keyboard.
b) a mouse.
c) a speaker.
d) a microphone.
(b) Which one of the following typically provides data persistence without electricity?
a) I, II only
b) I, III only
c) II, III only
d) I, II, III
a) An editor converts program files into an executable program; a compiler allows program
files to be written and stored
b) An editor allows program files to be written and stored; a compiler produces an organized
list of files
c) An editor allows program files to be written and stored; a compiler produces an indexed
database of terms and keywords
d) An editor allows program files to be written and stored; a compiler converts
program files into an executable program
(g) These two lines of code do not produce the same output. Why?
System.out.println(7 + 3);
System.out.println("7 + 3");
a) Arithmetic calculations cannot take place within the println method call.
b) In fact, the two statements do produce the same output.
c) The quotes cause the second expression to be treated as a string.
d) Because there are no escape characters.
a) System.out.print(;
b) System.out.print()
c) System.out.printl();
d) System.out.println();
System.outt.println("Hello");
(k) Which one of the following code snippets compiles without errors and displays the output
“Hello Good Day!” on the screen?
a) System.out.print("Hello ");
System.out.println("Good Day!");
b) System.out.print("Hello );
System.out.println("Good Day!");
c) System.out.print("Hello ");
System.out.println("Good Day!")
d) System.out.print("Hello ");
System.out.println(Good Day!");
System.out.print("d4 = ");
System.out.println(d4);
System.out.print("d3 = ");
System.out.println(d3);
System.out.print("d2 = ");
System.out.println(d2);
System.out.print("d1 = ");
System.out.println(d1);
}
}
d4
=
9
d3
=
4
d2
=
6
d1
=
5
public class P2 {
public static void main(String args[]) {
double p=8.0, q=6.0, r=12.0, num1, num2;
num1 = p + q / r;
num2 = p * q + p * r % 2;
System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
}
}
num1 = 8.5
num2 = 48.0
4. Write
a
Java
program
that
computes
the
maximum
and
average
of
3
numbers
that
are
read
from
keyboard
input.
A
sample
run
is
shown
below:
import java.util.Scanner;
import java.lang.Math;
sc.close();
}
5. You have been asked to develop an algorithm to calculate the total cost of a purchase
order that contains several T shirts. The cost of each T shirt and the tax rate is known.
The standard shipping charge for the entire order is $5.75, and the special delivery charge
is $23.65. In addition, there is no tax on the shipping cost. Which of the following is the
correct pseudocode for the required algorithm?
c) If standard shipping
Shipping cost = 5.75
Else
Shipping cost = 23.65
For each T shirt on the purchase order:
Order cost = order cost + T shirt cost + shipping cost
Total purchase order cost = order cost * tax rate
d) If special delivery
Shipping cost = 5.75
Else
Shipping cost = 23.65
For each T shirt on the purchase order:
Order cost = order cost + T shirt cost
Total purchase order cost = order cost * tax rate + shipping cost
6. Imagine you are planning to purchase a new cable TV dish. You are considering two cable TV
dishes that have different purchase prices. Each channel service provider charges a different rate
for each month that the cable TV dish is used. To determine which cable TV dish is the better
buy, you need to develop an algorithm to calculate the total cost of purchasing and using each
cable TV dish. What are all of the inputs that you need for this algorithm?
a) The cost of each cable TV dish and the rate for each month for using each cable TV dish
b) The cost of each cable TV dish and the number of months provided with each cable TV dish
c) The cost of each cable TV dish, the rate per month for using each cable TV dish, and the
number of months provided with each cable TV dish
d) The cost of each cable TV dish, the rate per month for using each cable TV dish,
and the number of months you would use the cable TV dish.
someNum = 0
Repeat the following steps 50 times
Input variable1
if variable1 > someNum
someNum = variable1
Print someNum
Input:
Get a value for n, the size of the list
Get values for A1, A2, …, An
Method:
Set largestSoFar to A1
Set smallestSoFar to A1
Set i to 2
While (i <= n) do
If (Ai > largestSoFar) Then
Set largestFoFar to Ai
If (Ai < smallestSoFar) Then
Set smallestSoFar to Ai
Add 1 to i
End of loop
Output:
Print largestSoFar and smallestSoFar