Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Computer programming- I (CS 1301) Tutorial - #03

Tutorial - #03 Selection Statements Evaluation:

Student Name & Number


Instructor: Maha

Q1: Fill in the blanks in each of the following:

1. Every java program begins execution at the method (main).


2. A (left brace { ) begins the body of every method and a (right brace } ) ends the body.
3. Every java statement ends with a (semicolon ; ).
4. Arithmetic operations are on the same level of precedence as multiplication ( division /,
modulus % )
5. When parentheses are nested, which set of parentheses is evaluated first in an arithmetic
expression (innermost pair )
6. The if single-selection statement selects or ignores one or more actions.
7. The if…else double-selection statement performs one action when the condition is true and a
different action when the condition is false.
8. A program can test multiple cases with nested if…else statements.
9. The switch statement is called a multiple-selection statement because it selects among many
different actions or groups of actions.
10. All programs can be written in terms of three types of control structures: sequence, selection
and repetition.

Q2: State whether each of the following is true or false. If false, explain why.

1. Comments cause the computer to print the text after the // on the screen when the program is
executed (False). Comments do not cause any action to be performed when the program is
executed. They’re used to document programs and improve their readability.
2. The escape sequence \n, causes the cursor to position to the beginning of the next line on the
screen (True).
3. All variables must be declared before they’re used (True).
4. All variables must be given a type when they’re declared (True).
5. Java considers the variables number and NuMbEr to be identical (False). java is case sensitive,
so these variables are unique.
6. The modulus operator (%) can be used only with integer operands (True).
7. The arithmetic operators *, /, %, + and – all have the same level of precedence (False). The
operators *, / and % have the same precedence, and the operators + and – have a lower
precedence.
8. A java program that prints three lines of output must contain three statements using
System.out.println (False). One statement with System.out.println and multiple \n escape
sequences can print several lines.
9. The if statement expects one statement in its body. To include several statements in the body
of an if (or the body of an else for an if…else statement), enclose the statements in braces.
(True).
10. A set of statements contained within a pair of parentheses is called a block. False. A set of
statements contained within a pair of braces ({ and }) is called a block.

Q3: Identify and correct the errors in each of the following sets of code:

1
Computer programming- I (CS 1301) Tutorial - #03
a)
if ( gender == 1 )
System.out.println( "Woman" );
else;
System.out.println( "Man" );
Error: The semicolon after else results in a logic error. The second output statement will always
be executed.
Correction: Remove the semicolon after else.

b)
if ( age >= 65 );
System.out.println( "Age is greater than or equal to 65" );
else
System.out.println( "Age is less than 65" );
Error: The semicolon after if results in a logic error. The first and second output statement will
always be executed.
Correction: Remove the semicolon after if.

Q4: State the output for each of the following when:


x is 5, y is 6 x is 11, y is 9 x is 9, y is 11
##### $$$$$ ***** a) if ( x < 10)
$$$$$ $$$$$ if ( y > 10)
System.out.println("*****");
else
System.out.println("#####");
System.out.println("$$$$$");
Nothing ##### ***** b) if ( x < 10)
$$$$$ {
if ( y > 10(
System.out.println("*****");
}
else
{
System.out.println("#####");
System.out.println("$$$$$");
}

Q5: Modify the following code to produce the output shown. You must not make any
changes other than inserting braces.
if ( y == 8 )
if ( x == 5 )
System.out.println ("@@@@@");
else
System.out.println ("#####");
System.out.println ("$$$$$");
System.out.println ("&&&&&");

2
Computer programming- I (CS 1301) Tutorial - #03
a) Assuming x = 5 and y = 8, the following output is produced.
@@@@@
$$$$$
&&&&&
if ( y == 8 )
if ( x == 5 )
System.out.println ("@@@@@");
else
System.out.println ("#####");
System.out.println ("$$$$$");
System.out.println ("&&&&&");

b) Assuming x = 5 and y = 8, the following output is produced.


@@@@@
if ( y == 8 )
if ( x == 5 )
System.out.println ("@@@@@");
else
{
System.out.println ("#####");
System.out.println ("$$$$$");
System.out.println ("&&&&&");
}

c) Assuming x = 5 and y = 8, the following output is produced.


@@@@@
&&&&&
if ( y == 8 )
if ( x == 5 )
System.out.println ("@@@@@");
else
{
System.out.println ("#####");
System.out.println ("$$$$$");
}
System.out.println ("&&&&&");

d) Assuming x = 5 and y = 7, the following output is produced. [Note: The last three
outputstatements after the else are all part of a block.]
#####
$$$$$
&&&&&
if ( y == 8 )
{
if ( x == 5 )
System.out.println ("@@@@@");
}
else
System.out.println ("#####");
System.out.println ("$$$$$");
System.out.println ("&&&&&");

3
Computer programming- I (CS 1301) Tutorial - #03

Q6: What is output of the following program for each of the input values 5, 7, 100, –7, 0?

Program
import java.util.Scanner;
public class Q6 {
public static void main( String args[] ){
int number;
Scanner input = new Scanner( System.in );

System.out.println( "Enter integer: " );


number = input.nextInt();

if ( number != 7 )
System.out.print( "Welcome " );

if ( ( number % 5 ) == 0 )
System.out.println( "To Java Programming" );
}
}
Output
Enter integer: 5
Welcome To Java Programming
Enter integer: 7
Enter integer: 100
Welcome To Java Programming
Enter integer: -7
Welcome
Enter integer: 0
Welcome To Java Programming

4
Computer programming- I (CS 1301) Tutorial - #03
Q7: What is output of the following program?

Program
public class Q7{
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1: monthString = "January";
break;
case 2: monthString = "February";
break;
case 3: monthString = "March";
break;
case 4: monthString = "April";
break;
case 5: monthString = "May";
break;
case 6: monthString = "June";
break;
case 7: monthString = "July";
break;
case 8: monthString = "August";
break;
case 9: monthString = "September";
break;
case 10: monthString = "October";
break;
case 11: monthString = "November";
break;
case 12: monthString = "December";
break;
default: monthString = "Invalid month";
break;
}
System.out.println(month + " is "+ monthString);
}
}
Output
8 is August

5
Computer programming- I (CS 1301) Tutorial - #03

Q8: What is output of the following program:

Program
public class Q8 {
public static void main(String[] args) {
char grade = 'B';
switch(grade)
{
case'A':
System.out.println("Excellent!");
break;
case'B':
case'C':
System.out.println("Well done");
break;
case'D':
System.out.println("You passed");
break;
case'F':
System.out.println("Better try again");
break;
default:
System.out.println("Invalid grade");
}
System.out.println("Your grade is "+ grade );
}
}
Output
Well done
Your grade is B

6
Computer programming- I (CS 1301) Tutorial - #03

Q9: What is output of the following program:

Program
public class Q8 {
public static void main(String[] args) {
int a = 60;
if( a == 10 )
{
System.out.println("Value of a is 10");
}
else if( a == 20 )
{
System.out.println("Value of a is 20");
}
else if( a == 30 )
{
System.out.println("Value of a is 30");
}
else
{
System.out.println("Value of a is not matching");
}
System.out.println("Exact value of a is : "+ a );
}
}
Output
Value of a is not matching
Exact value of a is : 60

7
Computer programming- I (CS 1301) Tutorial - #03
Q10. Write an application that inputs three integers from the user and displays the sum,
average, product, smallest and largest of the numbers. [Note: The calculation of the
average in this exercise should result in an integer representation of the average. So if the
sum of the values is 7, the average should be 2, not 2.3333….]
Problem-Solving Tips:

1. Prompt the user for three integer values and use Scanner method nextInt to read them
into their respective int variables.
2. Use a series of if statements to determine the smallest and largest numbers. You must
use relational operators in the if conditions to compare two numbers at a time.
3. Calculate the sum, product and average, and assign them to variables called sum,
product and average, respectively. Then, display the results.
4. Test your program thoroughly using different test inputs and determine whether your
program produces the correct results. Try entering 10, 20, and 30 and see if your results
match the sample output above.

Sample Output:

Enter first integer: 10


Enter second integer: 20
Enter third integer: 30
For the numbers 10, 20 and 30
Largest is 30
Smallest is 10
Sum is 60
Product is 6000
Average is 20

Program
import java.util.Scanner;
public class Q10 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int number1, number2,number3,largest,smallest,sum,product;
double average;

System.out.print("Enter first integer: " );


number1 = input.nextInt();
System.out.print("Enter second integer: ");
number2 = input.nextInt();
System.out.print("Enter third integer: ");
number3 = input.nextInt();

// determine largest value


largest = number1; // assume number1 is the largest
if (number2 > largest)
largest = number2;
if (number3 > largest)
largest = number3;

// determine smallest value


smallest = number1; // assume number1 is the smallest

if (number2 < smallest)


8
Computer programming- I (CS 1301) Tutorial - #03
smallest = number2;
if (number3 < smallest)
smallest = number3;

// perform calculations
sum = number1 + number2 + number3;
product = number1 * number2 * number3;
average = sum / 3.0;

// print results
System.out.printf("\nFor the numbers %d, %d and %d\n",
number1, number2, number3);
System.out.printf("Largest is %d\n", largest);
System.out.printf("Smallest is %d\n", smallest);
System.out.printf("Sum is %d\n", sum);
System.out.printf("Product is %d\n", product);
System.out.printf("Average is %d\n", average);
}
}

You might also like