CSE110 Midterm Review Lab (No Submission)
CSE110 Midterm Review Lab (No Submission)
(No Submission)
BRANCHING
❖ Problem 1: Calculate a car's average consumption being provided the total distance
traveled (in Km) and the spent fuel total (in liters).
Explanation: First input 500 represents the total distance (in Km) and the
second one is a floating point number representing the spent fuel (in Litre).
❖ Problem 2: Using the following table, write a program that reads a code and the amount of
an item. After, print the value to pay. This is a very simple program with the only intention of
practicing selection commands.
2 X-Salad $4.50
3 X-Bacon $5.00
4 Toast $2.00
5 Soda $1.50
3 Item: X-Bacon
2 Quantity: 2
Total: $10.00
Explanation: First input is the product code and second input is the
quantity of the item. Two X-Bacon were ordered, so the value is $10.00.
❖ Problem 3: Read the four integer values corresponding to the x and y axes of two points,
p1 (x1, y1) and p2 (x2, y2) and calculate the distance between them. Also, show whether
the slope created from this line is positive or negative or zero. The formulas are given
below:
Distance = √ ❑
( y 2− y 1)
Slope =
(x 2−x 1)
Explanation: The first two inputs are x1, y1 and the second two inputs are
x2 y2. The result is calculated accordingly.
❖ Problem 4: Read 3 floating-point numbers (double) A, B and C and Find the two values of
X. If it's impossible to calculate the roots because of a division by zero or a square root of a
negative number, print the message “Impossible to calculate”.
−B ± √ ❑
X=
❑
Sample Input Sample Output
Explanation: First value = 10, change = 20, last value = 134. The terms of the series
increase by 20, and stop at 130 because 140 would exceed 134. You have to print
the commas as well.
0 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200
100
1234
❖ Problem 6: Write Java code of a program that asks the user the value n as input and then
prints the value of y. Hint: Every 4th term is a negative number.
4 Y = 1.5833
❖ Problem 7: Write a Java code that asks the user for the value of N as input and then prints
the value of Y.
y = 3 - 5 + 7 - 9 ….. nth term
5 7
Explanation: If the user gives n=1, print 3. If the user gives n=2, print -2. If the user
gives n=3, print 5 and so on. For example, for the input 5 we’ll add up the first 5
numbers of the series, so 3-5+7-9+11 = 7.
❖ Problem 8: Write a java program that takes an integer input from the user and finds the
Collatz sequence of that number.
➢ Collatz Number:
■ If the current number is even, divide it by 2.
■ If the current number is odd, multiply it by 3 and add 1.
■ Repeat the process until the number becomes 1.
6 6,3,10,5,16,8,4,2,1 6/2 -> 3*3+1 -> 10/2 -> 5*3+1 -> 16/2 ->8/2 -> 4/2 -> 2/2 -> 1 (stop)
❖ Problem 9: Write a java program that takes an integer input from the user and calculates
the sum of its digits. If the summation is even, please print “The number is even.” otherwise
print “The number is odd.”
❖ Problem 10: Write a java program that takes a binary number as input from the user and
prints its decimal equivalent.
5 Max: 12
12 Min: 8
-8 Average: 10
19
8
-1
Explanation:
At first the user gave 5 as the input which indicates that the user
will provide 5 numbers. Then 5 numbers were taken as inputs.
Among these, only 12 and 8 are even positive numbers.
❖ Problem 12: Write a java program that keeps on taking user inputs until the user provides 0
and prints the maximum, minimum, and average of all the even positive numbers given by
the user. If no even positive number is given, the average should be zero.
12 Max: 12
-8 Min: 8
19 Average: 10
8
-1
0
Explanation:
After giving 5 numbers, the user provided 0, therefore, no more
inputs were taken. Among these, only 12 and 8 are even positive
numbers.
Reference:
https://judge.beecrowd.com/en
Tracing
➔ Problem 13: What is the value of each of the following arithmetic expressions in Java?
Explain your answer:
20/6
10/4.0
8/2-9+5
(8/2-9)+5
Math.pow(36,0.5)
Math.pow(25,1/2)
Math.pow(1,7)-Math.pow(4,1/2.0)
Math.pow(2,4)+Math.pow(3,2)
Math.pow(3,3)+Math.pow(4,2)
5+6-4*2/2+1
5+6-4*(2/2+1)
5 % 3
5.0 / 3
"23" + 3 + 4
4.0+2.0*(5.0+4.0/3.0)
4-(5+4/3)/3.0%2
4%21
-4%21
4%-21
-4%-21
12/5
-12/5
12/-5
-12/-5
1. public class T1 {
2. public static void main(String[] args) {
3. boolean var1=false,var2=false,var3=false,var4=false,var5=false;
4. boolean var6=false,result1=false,result2=false,result3=false,result4=false;
5. boolean result5=false, result6=false, result7=false, result8=false;
6. boolean result9=false, result10=false;
7. var1=(!false || false) && true;
8. var2=var1 && true;
9. var3=false && !true;
10. var4=true;
11. var5=false;
12. var6=var3 && true;
13. result1=(var1 && var2) && ( 40 % 3 > 45) || (var5 && var6);
14. result2=(var1 || var2) || (result1 && false);
15. result3=(var1 && result1) || result2 || var5;
16. result4=(var1 || var2) || ((var3 && var1) && false);
17. result5=(var1 && var2) && (result3 || var1);
18. result6=((var3 || !var2) && (result5)) || true;
19. result7=(var4 && result1) && ((result1 && false) || true);
20. result8=((var1 && result3) && (!var5 || var6)) && true;
21. result9=((result2 && var2) || (!result7 && var1)) && !false;
22. result10=!(var1 && true);
23. }
24. }
result1
result2
result3
result4
result5
result6
result7
result8
result9
result10
➔ Problem 16: Write outputs of following codes:
1 public class T2 {
2 public static void main(String[] args) {
3 int p = 5;
4 int q = 6;
5 int r = 9;
6 int sum = 0;
7 if (p < 12) {
8 System.out.println(r + 2);
9 } else {
10 System.out.println(r + p);
11 }
12
13 if (q > 20){
14 System.out.println(r + 19);
15 } else if (q <= 6) {
16 System.out.println(q + 3);
17 } else{
18 System.out.println(p + q + r);
19 }
20
21 if (r > 15) {
22 System.out.println(r);
23 } else if (r == 0) {
24 System.out.println(p + q);
25 } else {
26 System.out.println(p);
27 }
28
29 if (sum != 0) {
30 System.out.println(3);
31 } else {
32 System.out.println(sum + 32);
33 }
34
35 if(p > 0 && r < 10){
36 System.out.println(p + r);
37 } else {
38 System.out.println(p - r);
39 }
40 }
41 }
➔ Problem 17: Write outputs of following codes:
3 int a = 10;
4 int b = 150500;
7 sum += 1.2;
9 if(a>=6){
10 System.out.println((int) sum);
11 }
12 else{
13 System.out.println(sum);
14 }
15 b = b/a;
16 }
17 }
18 }
➔ Problem 18: Write outputs of following codes:
4 while(0<p%10){
5 if(m % 10 == 0){
6 sum = sum * m % n + p / n ;
7 }
8 else{
9 if(m % 4 == 0){
10 sum += n + (--m);
11 }
12 else{
13 sum -= m--;
14 }
15 }
16 p+=1;
17 System.out.println(sum);
18 }
20 }
21 }
➔ Problem 19: Write outputs of following codes:
4 int x = 2, y = 2, z = 3;
5 while (check){
6 y = 4 / x % 3 + z * y - 5;
7 if(y > 10 || x==7){
8 z += 3;
9 break;
10 }
11 if(4+x%3 > 5){
12 x %= y + (z--) + z;
13 System.out.println(x);
14 }
15 else{
16 y += x + (--z) + y;
17 System.out.println(y);
18 }
19 x++;
20 System.out.println(x + y);
21 }
22 }
23 }