0% found this document useful (0 votes)
18 views11 pages

CSE110 Midterm Review Lab (No Submission)

The document contains a series of programming problems designed for a CSE110 midterm review, focusing on concepts such as branching, loops, and arithmetic expressions in Java. Each problem includes sample inputs and outputs to illustrate the expected results, covering topics like average fuel consumption, item pricing, distance and slope calculations, and various series and sequences. Additionally, there are problems related to boolean expressions and output of code snippets to reinforce understanding of Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

CSE110 Midterm Review Lab (No Submission)

The document contains a series of programming problems designed for a CSE110 midterm review, focusing on concepts such as branching, loops, and arithmetic expressions in Java. Each problem includes sample inputs and outputs to illustrate the expected results, covering topics like average fuel consumption, item pricing, distance and slope calculations, and various series and sequences. Additionally, there are problems related to boolean expressions and output of code snippets to reinforce understanding of Java programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CSE110 Midterm Review Lab

(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).

Sample Input Sample Output

500 14.286 km/l


35.0

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.

CODE Item Price

1 Hot Dog $4.00

2 X-Salad $4.50

3 X-Bacon $5.00

4 Toast $2.00

5 Soda $1.50

Sample Input Sample Output

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)

Sample Input Sample Output

1.0 Distance: 4.4721


7.0 Slope: 0.5000
5.0 The slope is Positive
9.0

Sample Input Sample Output

2.5 Distance: 16.4575


-0.4 Slope: -0.5034
-12.2 The slope is Negative
7.0

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

10.0 Root#1 = -0.29788


20.1 Root#2 = -1.71212
5.1

Sample Input Sample Output

0.0 Impossible to calculate


20.0
5.0

Explanation: The three inputs are values of A,B,C respectively. The


results are calculated accordingly.
LOOP
❖ Problem 5: Write a Java program that will ask the user for the first value, change, and last
value of an arithmetic series, and then print the series.

Sample Input Sample Output

10 10, 30, 50, 70, 90, 110, 130


20
134

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.

Sample Input Sample Output

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.

y = 1 + 1/2 + 1/3 – 1/4 + 1/5 + 1/6 + 1/7 - 1/8 + .... 1/n

Sample Input Sample Output

4 Y = 1.5833

Explanation: The result is calculated according to the formula.

❖ 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

Sample Input Sample Output

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.

Sample Sample Output Explanation


Input
4 4,2,1 4/2 -> 2/2 -> 1 (stop)

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.”

Sample Input Sample Output


54637 The sum is odd.

Explanation: 5+4+6+3+7=25 which is odd.

2754 The sum is even.

Explanation: 2+7+5+4=18 which is even.

❖ Problem 10: Write a java program that takes a binary number as input from the user and
prints its decimal equivalent.

Sample Sample Explanation


Input Output
1111 15 1(23 )+ 1(22 )+ 1(21 )+ 1(20 ) = 15

0101 5 0(23 )+ 1(22 )+ 0(21 )+ 1(20 ) = 5


❖ Problem 11: Write a java program that asks the user how many inputs they want to provide
and then takes that many inputs 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.

Sample Input Sample Output

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.

Sample Input Sample Output

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

➔ Problem 14: Write outputs of following codes:


int p, y = 25, w = 14, j = 12, z = 2, c = 3;
double d = 42;
p = y / 3 % 2;
System.out.println(y - p / 2);
j*=2;
w = w / 2 * 3 - j;
System.out.println(w % 2 + j);
z+= 8;
d/=2;
c = z % c;
d = 1 + d / c + 21;
System.out.println(d / 2 + 3 + "c");
c=c++ + c-- + z++ + z-- + ++c;
System.out.println(c * 2 + d);
➔ Problem 15: Show the values of the result variables in the above program:

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:

1 public class Task20 {

2 public static void main(String[] args) {

3 int a = 10;

4 int b = 150500;

5 double sum = a/1.0;

6 while (b/a != 0){

7 sum += 1.2;

8 sum = b % 3 + (-- a) * 2 + sum;

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:

1 public class Task21{

2 public static void main(String[] args) {

3 int m = 17, n = 13, p = 1, sum = 30;

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 }

19 System.out.println(!(n%13 == 0) && !false ||


p>10);

20 }

21 }
➔ Problem 19: Write outputs of following codes:

1 public class Task22{


2 public static void main(String args[]){
3 boolean check = true;

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 }

You might also like