Tutorial # 4 Q1. Evaluate the logical (Boolean) expression in the following exercise 1 int num1 = 3, num2 = 2; (num1 > num2) 2 double hours = 12.8; (hours > 40.2) 3 int funny = 7; (funny!= 1) 4 double y = -12.8; (y >= 0.0) 5 Boolean flag = true; int a =2, b = 5, c= 10; (a* b <= c &&! flag ) Answer true false true false false Q2. Evaluate the following expressions. Use the following values: x = false, y = false, z = true 1!(x y) z True 2 x && z && y False 3! x ( y!z) True 4 x (z && ( y x)) False 5 true!z && y True 6! ( x y ) x True 7 z && x && y False 8 x (y && (x z)) False 9 false! x && y False Answer Q3. Assume x = 6, found = true ; what is printed in each of the following segments: a. if ( x == 6) System.out.println("A match is found."); System.out.println("A match was not found "); System.out.println("Seqence continues after selection is complete.");
Answer (output): A match is found. Seqence continues after selection is complete. b. if (!(x < 8)) System.out.println("x is within the range."); System.out.println("true statement."); System.out.println("x is out of range."); System.out.println("false statement."); System.out.println("Seqence continues after selection is complete."); Answer (output): x is out of range. false statement. Seqence continues after selection is complete. c. if ( x >= 5 && found) System.out.println("Have a good day "); System.out.println("The value x is "+ x );
Answer (output): The value x is 6 d. if ( x > 0 && x < 16) System.out.println("x is in range."); System.out.println("x is not in range "); Answer (output): x is in range. e. int t = 78; int m = 6; if ( t >= 70 && m >= 6) System.out.println("statement 1"); if ( x == 6 ) System.out.println("statement 2 "); System.out.println("statement 3"); Answer (output): statement 1 Q4: int value = 0; switch (x) case 1: value +=4; break; case 2: value +=3; case 3: value +=2; default: value++; System.out.print("x = " + x + ", value = + value);
What will be output if x = 2? x = 2, value = 6 What will be output if x = 1? x = 1, value = 4 What will be output if x = 3? x = 3, value = 3 What will be output if x = 5? x = 5, value = 1 Q5: The cost of a call from Riyadh to Jeddah is calculated as follows: Connection fee, 2 R.S to first three minutes, and 0.5 for each additional minute. Write a program that prompts the user to enter the number of minutes the call lasted and outputs the amount due. import java.util.scanner; public class CostOfCall public static void main(string[] args) Scanner input = new Scanner (System.in); System.out.print("Enter the number of minutes"); int minutes = input.nextint(); if (minutes <= 3) System.out.println("Cost = 2R.S"); int extra_minutes = minutes - 3 ; double cost = (extra_minutes * 0.5 ) + 2; System.out.println("Cost = " + cost +"R.S");
Q6: Write a program that prompts the user to input a number. The program should then output a message saying whether the number is positive, negative, or zero. import java.util.scanner; public class Number public static void main(string[] args) Scanner input = new Scanner (System.in); System.out.print("Enter a number: "); int num = input.nextint(); if (num == 0) System.out.println("Zero"); if (num > 0) System.out.println("Positive"); System.out.println("Negative"); Suppose, when you run the following program, you enter the input 2 3 6 from the console. What is the output? public class Test public static void main(string[] args) java.util.scanner input = new java.util.scanner(system.in); double x = input.nextdouble(); double y = input.nextdouble(); double z = input.nextdouble(); System.out.println("(x < y && y < z) is " + (x < y ^ y < z)); System.out.println("(x < y y < z) is " + (x < y y < z)); System.out.println("!(x < y) is " +!(x < y)); System.out.println("(x + y < z) is " + (x + y < z)); System.out.println("(x + y > z) is " + (x + y > z)); run: 2 3 6 (x < y && y < z) is false (x < y y < z) is true!(x < y) is false (x + y < z) is true (x + y > z) is false BUILD SUCCESSFUL (total time: 5 seconds)
Suppose the input is 5. ( Assume that all variables are properly declared).what is the output of the code: num=console.nextint(); If(num>5) System.out.println(num); num=0; System.out.println( Num is zero ); Num is zero Circle the best answer. a. If (6<2*5) System.out.print( Hello ); System.out.print( There ); Outputs the following : (i) Hello There (ii) Hello (iii)hello (iv) There There b. if (7<8) System.out.println( 2 4 6 8 ); System.out.println( 1 3 5 7 ); Outputs the following (i) 2 4 6 8 (ii) 1 3 5 7 (iii) none of these 1 3 5 7 What is the output? if (5<3) System.out.print("*"); if(7==8) System.out.print("&"); System.out.print("$"); $
Suppose x = 3 and y = 2; show the output, if any, of the following code. What is the output if x = 3 and y = 4? What is the output if x = 2 and y = 2? if (x > 2) if (y > 2) z = x + y; System.out.println("z is " + z); System.out.println("x is " + x); Suppose that, when you run the following program, you enter the input 2 3 6 from the console. What is the output? public class Test public static void main(string[] args) java.util.scanner input = new java.util.scanner(system.in); double x = input.nextdouble(); double y = input.nextdouble(); double z = input.nextdouble(); System.out.println((x < y && y < z)? "sorted" : "not sorted"); Rewrite the following if statements using the conditional operator if (ages >= 16) ticketprice = 20; ticketprice = 10; ticketprice= (ages>=16)? 20 :10 Rewrite the following conditional expressions using if- statements. tax = (income > 10000)? income * 0.2 : income * 0.17 + 1000; Suppose the input is 3. What is the value of beta after the following java code executes(assume that all variables are properly declared) beta=console.nextint(); switch(beta) case 3: beta=beta+3; case 1: beta++;break; case 5:beta=beta+5; case 4: beta=beta+4;
Find the error in the code: public static void main(string[] args) double m=8.1; String x="a"; switch (m) case 1: System.out.print("a");break; case 8: System.out.print("b");break; default: System.out.print("none"); public static void main(string[] args) int n=1 switch(n<=2) case 1: System.out.print("Draw"); break; case 2: System.out.print("win"); break; case3: System.out.print("lose") If n>2 System.out.print( ++); Which of the following is True or False. 1. Conditional operator (?:) that can be used in place of an if statement. ( ) 2. In Conditional operator,the second operand (between the? and :) is the value of the conditional expression if the Boolean expression is true ( ) 3. The Java compiler always associates an with the immediately preceding if( ) 4. switch provides a mechanism for testing ranges of values ( ) 5. The result of a logical expression cannot be assigned to an int variable( ) 6. A program can test multiple cases with nested if statements ( ) 7. The java rational operator for equality is = ( ) 8. In java, Boolean variables are used to store the value of a logical expression ( ) 9. A break statement causes an immediate exit from the switch structure ( ) 10. The expression: ( ch>= A && ch<= z ) evaluates to false if either ch< A or ch>= Z ( )