Download Operators Precedence Table - Midterm Examination | CSE 8B and more Exams Computer Science in PDF only on Docsity!
Signature _____________________ Name ________________________
cs8f ____ Student ID ____________________
CSE 8B
Midterm
Winter 2011
Page 1 ___________ (19 points)
Page 2 ___________ (16 points)
Page 3 ___________ (12 points)
Page 4 ___________ (17 points)
Page 5 ___________ (20 points)
Page 6 ___________ (16 points)
Total ___________ (100 points = 95 base points + 5 points EC [5%])
(95 points = 100%)
This exam is to be taken by yourself with closed books, closed notes, no electronic devices.
You are allowed one side of an 8.5"x11" sheet of paper handwritten by you.
(Partial) Operator Precedence Table
Operators Associativity
! ++ -- (pre & post inc/dec) right to left
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
= right to left
Which of the following can be used in a Java program as identifiers? ( Circle all identifiers that are legal.)
five%Off For ABC's A-Z giveMe5 'x'
5PercentOff fivePercentOff FIRST_NAME main character if
What is the output of the following program?
public class Tricky { public static void main( String[] args ) { foo1(); System.out.println( "main1" ); foo2(); System.out.println( "main2" ); foo3(); System.out.println( "main3" ); }
public static void foo1() { System.out.println( "A" ); }
public static void foo2() { System.out.println( "B" ); foo3(); System.out.println( "C" ); }
public static void foo3() { System.out.println( "D" ); foo1(); System.out.println( "E" ); } }
How many tokens are in the following input? _________
Hello there,how are you? - -9 ++x I am "very well", thank you. i++ 123 4 56.7 (8 * 9) "11 + 12"
What gets printed as a result of the call Q4( 1, 3 )? __________
public void Q4( int a, int b ) { if ( (a > 0) && (b > 0) ) { if ( a > b ) { System.out.println( "A" ); } else { System.out.println( "B" ); } } else if ( (a < 0) || (b < 0) ) { System.out.println( "C" ); } else { System.out.println( "D" ); } }
Give an example of values passed as arguments to Q4() that would result in the method printing "D".
Q4( _____ , _____ );
What output is produced by the following program?
public class MysteryReturn
public static void main( String[] args )
int x = 2, y = 3, z = 4;
z = mystery( x, z, y );
System.out.println( x + " " + y + " " + z );
public static int mystery( int z, int x, int y )
z--;
x = 3 * y - z;
y = x - 1;
System.out.println( x + " " + y + " " + z );
return x;
Assume we have a Java source file with the source code for a class named class Boogie
Write the full Unix command to compile this Java program. _________________________________________
This command will produce a file named: _______________________________________________________
Write the full Unix command to run this as a Java application. _______________________________________
Assume a program had the following declarations:
String s1 = new String( "CSE8B" ); String s2 = new String( s1 ); String s3 = s1;
What results would be produced by evaluating the following expressions?
( s2 == s3 ) ____________ s2.equals( new String( s3 ) ) ____________
( s1 == s2 ) ____________ s2.equals( s1 ) ____________
What gets printed?
public class Loops { public static void main( String[] args ) { final int MAX = 8, MIN = 6; int i = -2, j = -3;
for (i = MAX; i > MIN; --i ) { j = 7; while ( j <= MAX ) { System.out.println( i + " " + j ); ++j; } }
System.out.println( i + " " + j );
} // end main() }
Consider the following code:
import java.util.Scanner; public class ScannerFun { public static void main( String[] args ) { Scanner console = new Scanner( System.in ); System.out.print( "Type something for me! " );
if ( console.hasNextInt() ) { int number = console.nextInt(); System.out.println( "Your IQ is " + number ); } else if ( console.hasNext() ) { String token = console.next(); System.out.println( "Your name is " + token ); } } }
What is the output when the user types the following values?
Jane _______________________________________________
56 _______________________________________________
56.2 _______________________________________________
Assume the two accessor methods getX() and getY() and the two mutator methods setX() and setY() discussed
in class are part of class Point to interact with the private instance variables x and y. What gets printed when the
following program is run?
public class PointMain { public static void main(String[] args) { Point p1 = new Point( 2, 5 ); p1.setX( 6 );
Point p2 = new Point( 3, 6 ); p2.setY( 9 );
System.out.println( p1.getX() + ", " + p1.getY() ); __________________
p2.setY( p2.getY() + 4 ); p2.setX( p2.getX() - 2 );
System.out.println( p2.getX() + ", " + p2.getY() ); __________________
// Tricky ... p2.setX( p1.getY() + 4 ); // NOTE which get/set methods are called p1.setY( p2.getX() - 2 );
System.out.println( p2.getX() + ", " + p2.getY() ); __________________
System.out.println( p1.getX() + ", " + p1.getY() ); __________________
int x = 5; p1 = new Point( x, x );
x = doubleDown( x, p1 ); System.out.println( x ); __________________
System.out.println( p1.getX() + ", " + p1.getY() ); __________________ }
public static int doubleDown( int x, Point p2 ) { x = p2.getX() + x; p2.setY( x + p2.getX() );
return x; } }
Assume class Point has the above instance accessor methods getX() and getY() along with mutator methods
setX() and setY() properly defined. Given the following array definition in some other class (not in class Point)
assuming all the array elements are properly initialized
Point[] points = new Point[42];
// Assume array points is properly and fully initialized with Point objects here
Without using any extra variables other than the variable points , write the single Java statement to change the
last Point's x value to be the same value as the first Point's y value. Do not use any magic numbers or integer
constants other than 0 or -1. Because each Point object's x and y are private, you cannot directly access them.
You must use the accessor/mutator methods.
_________________________________________________________________________________________;
Scratch Paper