




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





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 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:
System.out.println( ( 8 * 4 * 2 + 6 ) / 2 + 4 ); Your answer:
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:
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:
Enter an integer Enter an integer Enter an integer
Your answer:
Your answer:
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.]
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 }
value of the expression (5 + 3) * 2 to the variable: Make a corrections 1 int number; 2 number = 5 + 3 * 2; Your answer:
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:
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"); } } }