CMIS 102A Practice exam
Arithmetic Expressions
Show the output from these statements.
- __________
System.out.println( 2 + 3 * 4 );
- __________
System.out.println( 2 * 3 + 4 );
- __________
System.out.println( 4+3 * 2+1 );
- __________
System.out.println( 4 + 3*2 + 1 );
- __________
System.out.println( 10 / 5 );
- __________
System.out.println( 5 / 10 );
- __________
System.out.println( 5 / 2 );
- __________
System.out.println( 5.0 / 2 );
- __________
System.out.println( 5 / 2.0 );
- __________
System.out.println( 5.0 / 2.0 );
- __________
System.out.println( 7 % 3 );
- __________
System.out.println( 6 % 3 );
- __________
System.out.println( 3 % 3 );
- __________
System.out.println( 3 % 7 );
String Expression Exercise
Assume the following:
String t = "testing"; String a = "Auf Wiedersehen";
Show what is printed by each of these statements.
- __________
System.out.println( t.length() );
- __________
System.out.println( "Goodbye".length() );
- __________
System.out.println( t + 2 );
- __________
System.out.println( " real slow ".trim().length() );
- __________
System.out.println( t.substring(1) );
- __________
System.out.println( "Welcome".substring(2,4) );
- __________
System.out.println( t.toUpperCase() );
- __________
System.out.println( t.toUpperCase().toLowerCase() );
- __________
System.out.println( a.indexOf("A") );
- __________
System.out.println( a.indexOf("e") );
- __________
System.out.println( a.lastIndexOf("e") );
- __________
System.out.println( a.substring(a.indexOf(" ")+1) );
- __________
System.out.println( t.equals("TESTING") );
- __________
System.out.println( t.equalsIgnoreCase("TESTING") );
- __________
System.out.println( t.substring(1,3) == "bc" );
- __________
System.out.println( "t".compareTo(t) < 0 );
- __________
System.out.println( t.compareTo("test") >= 0 );
- __________
System.out.println( "t: \"" + t + "\"" );
- __________
System.out.println( "\\\\".length() );
Boolean Expression Exercises
Assume the following:
int small = 4; int large = 22; boolean t = true;
Show what is printed by each of the following statements.
- __________
System.out.println( 100 >= 1000 );
- __________
System.out.println( t );
- __________
System.out.println( large > small && small != 5 );
- __________
System.out.println( small < large && large == 10 );
- __________
System.out.println( large/2 >= small && small/2 == 3 );
- __________
System.out.println( large > large && small == 7 );
- __________
System.out.println( large > small || small != 5 );
- __________
System.out.println( small < large || large == 22 );
- __________
System.out.println( large/2 >= small || small/2 == 2 );
- __________
System.out.println( large > large || small == 7 );
If Statement Exercises
What is printed by the following code fragments?
Assume:
int alpha = 5; int n = 0;
-
n = 17; if (alpha < 2) { n = 18; } System.out.println( n );
Output = __________________
-
n = 36; if (alpha != 314159) n = 87; System.out.println( n );
Output = __________________
-
n = 12; if (alpha == 12345) n = 20; n = n + 1; System.out.println( n );
Output = __________________
-
n = 0; if (alpha > 6) { n = 100; } else { n = 200; } System.out.println( n );
Output = __________________
-
n = 0; if (alpha <= 6) { n = 100; } else { n = 200; } System.out.println( n );
Output = __________________
-
n = 0; if (alpha == 5) { n = 1; if (alpha > 44) { n = 2; } else { n = 3; } } else { n = 4; if (alpha > 1) { n = 5; } else { n = 6; } } System.out.println( n );
Output = __________________
Loop Statement Exercises
What is printed in each of these questions? If a loop never finishes (or an integer "wraps around" the maximum or minimum int values), put infinite in the answer space.
-
int n = 0; while (n < 4) { n = n + 2; } System.out.println( n );
Output = __________________
-
int n = 10; while (n >= 20) { n = n + 1; } System.out.println( n );
Output = __________________
-
int n = 0; int i = 0; while (i < 5) { if (i != 3 ) { n++; } else { n--; } i++; } System.out.println( n );
Output = __________________
-
String str = ""; while (str.length() <= 4) { str = str + "cha"; } System.out.println( str.length() );
Output = __________________
- Transform the following code into the equivalent
for
loop.sum = 0; int i = 1; while (i < 5) { sum = sum + i; i = i + 1; }
- Transform the following code into the equivalent using
while
loop.String greeting = "How are you?"; int n = greeting.length(); for (int i=0; i<n; i++) { char c = greeting.charAt(i); if (c != ' ') { System.out.print(c); } }