Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Similar documents
Tutorial 06. Conditional statement: if then, if else, switch

Midterm Examination (MTA)

b. Suppose you enter input from the console, when you run the program. What is the output?

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CONDITIONAL EXECUTION

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Selections. CSE 114, Computer Science 1 Stony Brook University

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Lecture 1 Java SE Programming

Programming with Java

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Topic 11 Scanner object, conditional execution

Object Oriented Programming. Java-Lecture 1

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Oct Decision Structures cont d

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

CS141 Programming Assignment #6

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Chapter 3 Selection Statements

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Building Java Programs

ITERATION WEEK 4: EXMAPLES IN CLASS

Chapter 4: Control Structures I

CCHAPTER SELECTION STATEMENTS HAPTER 3. Objectives

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

2.8. Decision Making: Equality and Relational Operators

AP CS Unit 3: Control Structures Notes

Building Java Programs

Lab Exercise 1. Objectives: Part 1. Introduction

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 45 Obtained Marks:

Introduction to Computer Science Unit 2. Notes

download instant at

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Chapter 3. Selections

Chapter 2: Basic Elements of Java

1. An operation in which an overall value is computed incrementally, often using a loop.

IEEE Floating-Point Representation 1

Java Programming Language. 0 A history

Control Structures in Java if-else and switch

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

Supplementary Test 1

Section 2.2 Your First Program in Java: Printing a Line of Text

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

Introduction to Computer Science Unit 2. Notes

In this chapter, you will:

1 Short Answer (10 Points Each)

Over and Over Again GEEN163

Expression statements are formed by adding a semicolon to the end of certain types of expressions.

Introduction to Computer Science Unit 2. Exercises

Topic 12 more if/else, cumulative algorithms, printf

CMSC131. Introduction to your Introduction to Java. Why Java?

Loops. CSE 114, Computer Science 1 Stony Brook University

Building Java Programs

AP Computer Science Unit 1. Programs

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Task #1 The if Statement, Comparing Strings, and Flags

Date: Dr. Essam Halim

Section 002 Spring CS 170 Exam 1. Name (print): Instructions:

CEN 414 Java Programming

Building Java Programs

AP COMPUTER SCIENCE A

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Building Java Programs

Values in 2 s Complement

Assignment 2.4: Loops

CSC 1051 Data Structures and Algorithms I

Topic 11 Scanner object, conditional execution

Building Java Programs

Repetition, Looping. While Loop

CS141 Programming Assignment #8

Java Coding 3. Over & over again!

Building Java Programs

King Saud University College of Computer and Information Sciences Computer Science Department

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

Elementary Programming

THE JAVA FOR STATEMENT

Day 2 : Intermediate Concepts 1 Examples

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Fundamentals of Programming Data Types & Methods

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

DM503 Programming B. Peter Schneider-Kamp.

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Topic 12 more if/else, cumulative algorithms, printf

CS141 Programming Assignment #5

Transcription:

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