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

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

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

AP COMPUTER SCIENCE A

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

Datatypes, Variables, and Operations

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

Operators Questions

Object Oriented Programming. Java-Lecture 1

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Control Structures in Java if-else and switch

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

1. Short circuit AND (&&) = if first condition is false then the second is never evaluated

H212 Introduction to Software Systems Honors

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

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 1, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

int x = 42, y = 33; //1 int z = x + y; //2 System.out.println(x + "+" + y + "=" + z); //3

1.Which four options describe the correct default values for array elements of the types indicated?

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

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

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Lara Technologies Special-Six Test

INDIAN LEARNERS OWN ACADEMY,kuwait

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

CIS November 14, 2017

Selected Questions from by Nageshwara Rao

CSC 1051 Arrays - Review questions

CSEN202: Introduction to Computer Science Spring Semester 2017 Midterm Exam

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

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

Contents. 8-1 Copyright (c) N. Afshartous

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name

Day 2 : Intermediate Concepts 1 Examples

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name: Question Value Score

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

16. Give a detailed algorithm for making a peanut butter and jelly sandwich.

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

Warmup : Name that tune!

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Building Java Programs

CS 113 PRACTICE FINAL

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Assignment 2.4: Loops

download instant at

Building Java Programs

Programming Basics. Digital Urban Visualization. People as Flows. ia

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

Building Java Programs

CS 170 Exam 1. Version: B Fall Name (as on OPUS):

Repetition CSC 121 Fall 2014 Howard Rosenthal

CSC 1051 Algorithms and Data Structures I. Final Examination May 12, Name: KEY. Question Value Score

Repetition with for loops

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

CSC 1051 Data Structures and Algorithms I

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

Final Examination Semester 3 / Year 2007

CS 170 Exam 1. Version: C Fall Name (as on OPUS):

CSE 142, Autumn 2008 Midterm Exam, Friday, October 31, 2008

COE 212 Engineering Programming. Welcome to Exam II Monday May 13, 2013

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

Introduction to Computer Science Unit 2. Exercises

The Hyderabad Public School Begumpet, Hyderabad, A.P.

Topic 4 Expressions and variables

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

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

CIS November 14, 2017

CIS 1068 Netflix Challenge New assignment posted soon Lab grades November 14, 2017

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

I. True/False: (2 points each)

CS111: PROGRAMMING LANGUAGE II

Object-Oriented Programming

CSC 1051 Algorithms and Data Structures I. Final Examination December 18, Name:

Selection Statements and operators

Research Group. 2: More types, Methods, Conditionals

Full file at

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 11, Name: KEY

CMPS 11 Introduction to Programming Midterm 1 Review Problems

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

MACS 261J Final Exam. Question: Total Points: Score:

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

Midterm Examination (MTA)

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

Topic 6 Nested for Loops

1 Short Answer (10 Points Each)

Introduction to Java Applications

CSC240 Static method, array, and loop.

Data Structure and Programming Languages

Logic & program control part 3: Compound selection structures

CS 106 Introduction to Computer Science I

CSC 1051 Algorithms and Data Structures I. Final Examination May 2, Name:

CIS October 19, 2017

Transcription:

College of Computer and Infmation Sciences CSC111 Computer Programming I Exercise 1: Tutial 06 Conditional statement: if then, if, switch What is the output of each of the following code fragments? (given the declaration int a=1, b=2, c=3;): A. if (6 < 2 * 5) System.out.print("Hello"); System.out.print(" There"); B. if ('a' > 'b' 66 > (int)('a')) System.out.println("#*#"); C. if (a < c) System.out.println("*"); if (a == b) System.out.println("&"); System.out.println("$"); E. if(a>b) {System.out.println("&&&&"); System.out.println(""); G. if (a < c) System.out.println("*"); if (a == c) System.out.println("&"); System.out.println("$"); I. if(a>b) if(a>c) System.out.println("1111"); D. if(a<b) System.out.println("&&&&"); System.out.println(""); F. int x = 100; int y = 200; if (x > 100 && y <=200) System.out.print(x+" "+y+" "+(x+y)); System.out.print(x+" "+y+" "+(2*x y)); H. if(a<b){ System.out.println(""); System.out.println("&&&&"); J. if(++a > b++ a > 0) c++; c ; System.out.println(a+" "+b+" "+c); System.out.println("2222"); Exercise 2: A. Write the java statement that assigns 1 to x if y is greater than 0 B. Suppose that sce is a variable of type double. Write the java statement that increases the sce by 5 marks if sce is between 80 and 90 C. Rewrite in Java the following statement without using the NOT (!) operat:

item =!( (i<10) (v>=50) ) D. Write a java statement that prints true if x is an odd number and positive E. Write a java statement that prints true if both x and y are positive numbers F. Write a java statement that prints true if x and y have the same sign ( /+) Exercise 3 Two programs are equivalent if given the same input they produce the same output. Which of the following programs are equivalent? Why? A. impt java.util.scanner; class TestPositive { { { B. impt java.util.scanner; class TestPositive { { C. impt java.util.scanner; 1

class TestPositive { if (x ==0) { Exercise 4 Convert the following switch statement into if statements then into if then statements: switch (day) { case 1: daystring1 = "Saturday"; case 2: case 3: daystring3 = "Monday"; case 4: daystring1 = "Tuesday"; case 5: default: daystring3 = "Invalid day"; 2

Tutial 06 Solutions Exercise 1: A. Hello There B. #*# C. * D. #### E. &&&& F. 100 200 0 G. * H. #### I. No output J. 1 3 4 Exercise 2: A. if (y > 0) x = 1; B. if (sce >= 80 && sce <=90) sce += 5; C. item = i >= 10 && i < 50 D. if (x % 2!= 0 && x > 0) System.out.println(true); System.out.println(x%2!=0 && x>0); // This prints false otherwise E. if (x > 0 && y > 0) System.out.println(true); System.out.println(x > 0 && y > 0); // This prints false otherwise F. if (x * y > 0) System.out.println(true); System.out.println(x * y > 0); // This prints false otherwise Exercise 3: Programs A and C are equivalent. Program B is different since it gives different output if input is a positive number greater than zero. F example, 3 Exercise 4: if : if (day == 1) { daystring1 = "Saturday"; 3

if (day == 2) if (day == 3) daystring3 = "Monday"; if (day == 4) { daystring1 = "Tuesday"; if (day == 5) daystring3 = "Invalid day"; if then: if (day == 1) { daystring1 = "Saturday"; if (day == 2) if (day == 3) daystring3 = "Monday"; if (day == 4) { daystring1 = "Tuesday"; if (day == 5) if (day < 1 day > 5) daystring3 = "Invalid day"; 4