Docsity

Lab Assignment - Programming Principles I | CS 2301, Exams of Computer Science

Toggle sidebar
bg1
Chapter_3 - Lab Assignments
Programming Output
For each of the given program segments, read the code, and write the output in the space
provided below each program. [Note: Do not execute these programs on a computer.]
1. What is the output of the following program?
1 public class Operator {
2
3public static void main( String args[ ] )
4 {
5 int x = 30;
6 int y = 2;
7
8System.out.println( x * y + 9 / 3 );
9 }
10 }
Your answer:
2. What is output by the following line of code?
System.out.println( ( 8 * 4 * 2 + 6 ) / 2 + 4 );
Your answer:
3. What is output by the following program for each of the input values 5, 7, 100, –7 and 0?
1 import java.util.Scanner;
2
3 public class Output
4 {
5 public static void main( String args[ ] )
6 {
7 int number;
8 Scanner input = new Scanner( System.in );
9
10 System.out.println( "Enter integer: " );
11 number = input.nextInt();
12
13 if ( number != 7 )
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lab Assignment - Programming Principles I | CS 2301 and more Exams Computer Science in PDF only on Docsity!

Chapter_3 - Lab Assignments

Programming Output For each of the given program segments, read the code, and write the output in the space provided below each program. [Note: Do not execute these programs on a computer.]

1. What is the output of the following program?

1 public class Operator { 2 3 public static void main( String args[ ] ) 4 { 5 int x = 30; 6 int y = 2; 7 8 System.out.println( x * y + 9 / 3 ); 9 } 10 } Your answer:

2. What is output by the following line of code?

System.out.println( ( 8 * 4 * 2 + 6 ) / 2 + 4 ); Your answer:

3. What is output by the following program for each of the input values 5, 7, 100, –7 and 0?

1 import java.util.Scanner; 2 3 public class Output 4 { 5 public static void main( String args[ ] ) 6 { 7 int number; 8 Scanner input = new Scanner( System.in ); 9 10 System.out.println( "Enter integer: " ); 11 number = input.nextInt(); 12 13 if ( number != 7 )

14 System.out.print( "Welcome " ); 15 16 if ( ( number % 5 ) == 0 ) 17 System.out.println( "To Java Programming" ); 18 } 19 } Your answer: Enter integer: Enter integer: Enter integer: Enter integer: Enter integer:

4. What is output by the following program? Assume the user enters 12 for one execution

of the program and 15 for a second execution. 1 import java.util.Scanner; 2 3 public class Compares 4 { 5 public static void main( String args[] ) 6 { 7 int integer; 8 Scanner input = new Scanner( System.in ); 9 10 System.out.println( "Enter an integer:" ); 11 integer = input.nextInt(); 12 13 if ( ( integer % 6 ) == 0 ) 14 System.out.println( "Hello" ); 15 else 16 System.out.println( "Good Bye" ); } } Your answer:

  1. What is output by the following program?

5. What is output by the following program?

Enter an integer Enter an integer Enter an integer

7. What is output by the program in Exercise 5 when x = 5, y = 25 and z = 99?

Your answer:

8. What is output by the program in Exercise 5 when x = 10, y = 9 and z = 8?

Your answer:

9. What is output by the program in Exercise 5 when x = 10, y = 10 and z = 99?

Your answer: Section: Correct the Code Determine if there is an error in each of the following program segments. If there is an error, specify whether it is a logic error or a compilation error, write the error in the program and write the corrected code in the space provided after each problem. If the code does not contain an error, write “no error.” [Note: There may be more than one error in each program segment.]

10. The following program should input the value of an integer into variable num:

1 import java.Scanner; 2 3 public class Output { 4 5 public static void main( String args[] ) 6 7 int num 8 Scanner input = Scanner( in ); 9 10 num = input.int(); 11 } 12 }

11. The following segment of code should declare an int variable number and assign the

value of the expression (5 + 3) * 2 to the variable: Make a corrections 1 int number; 2 number = 5 + 3 * 2; Your answer:

12. The following code should determine whether variable q is equal to 100 :

Make a corrections 1 int q = 100; 2 3 System.out.print( "q is" ); 4 5 if ( q = 100 ) 6 System.out.print( " equal to 100" ); if ( q! 100 ) System.out.print( " not equal to 100" ); Your answer:

13. The following code segment should determine whether an integer variable’s value is

less than zero. Make a corrections 1 int x = 9; 2 3 if ( x < 0 ); 4 System.out.println( "Variable x is less than zero" );

17. What is the printout of the following switch statement? char ch = 'c'; switch (ch) { case 'a': System.out.print(ch); case 'b': System.out.print(ch); case 'c': System.out.print(ch); case 'd': System.out.print(ch); } Your answer: 18. Assume that x is 3. Show the result of the following Boolean expressions. (true) && (3 > 4) Result is: _______________ ! (x > 5) && (x > 0) Result is: _______________ (x != 5) | | (x = = 0) Result is: _______________ (x >= 0) | | (x < 0) Result is: _______________ (x != 2) = = (x = = 4) Result is: _______________ 19. What is y after the following switch statement? int x = 0; int y = 0; int z = 0; switch (x + 1) { case 0: y = 0; case 1: y = 1; case 2: z = 2; default: y = -1; }

20. What is k after the following switch statement? class Switch1{ public static void main(String args[ ]) { int k = 10; switch(k){ case 5: System.out.println(" case k = 5"); break; case 10: System.out.println(" case k = 10"); break; case 15: System.out.println(" case k = 15"); break; default: System.out.println(" case default"); } } }