Programming For Beginners Java Tutorial#3 - Solution
Programming For Beginners Java Tutorial#3 - Solution
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.
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 ("&&&&&");
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 );
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
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
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:
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;
// 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);
}
}