Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Comparing Object References and Method Calls, Exams of Java Programming

Various java code snippets that demonstrate the comparison of object references using '==' operator, the 'equals()' method, and the swapping of object references using different methods. It also covers the concept of method call chaining and the execution flow of a java program.

Typology: Exams

2012/2013

Uploaded on 04/09/2013

gajkaraan
gajkaraan 🇮🇳

4.6

(14)

47 documents

1 / 13

Toggle sidebar

Related documents


Partial preview of the text

Download Java Programming: Comparing Object References and Method Calls and more Exams Java Programming in PDF only on Docsity! Signature _____________________ Name ________________________ cs11f ____ Student ID ____________________ CSE 11 Final Fall 2011 Page 1 ___________ (17 points) Page 2 ___________ (25 points) Page 3 ___________ (31 points) Page 4 ___________ (14 points) Page 5 ___________ (10 points) Page 6 ___________ (19 points) Page 7 ___________ (20 points) Page 8 ___________ (12 points) Page 9 ___________ (31 points) Page 10 ___________ (9 points) Total ___________ (188 points = 179 base points + 9 points EC [5%]) (100%) This exam is to be taken by yourself with closed books, closed notes, no electronic devices. You are allowed both sides of an 8.5"x11" sheet of paper handwritten by you. 1 (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 1) Which of the following are valid Java identifiers? (Circle your answer(s).) FiveStar Five-Star 5_Star INT 5Star Five_Star Five***** integer 2) Using the operator precedence table above, evaluate each expression and state what gets printed. int a = 5, b = -1, c = 3; System.out.println( !( a + b > c ) ); ________ System.out.println( a + b * c - c / a % c ); ________ System.out.println( a / c + a % c ); ________ System.out.println( !(a > 4 || b <= 6) == a >= 4 && c > 6 ); ________ 3) What are the values of the indicated variables after the following code segments are executed? Remember short-circuit evaluation with && and ||. What is the equivalent Java expression for the following expression such that no ! operators are used? !( x < -10 && y != 7 ) ____________________________________________ int x = 7, y = 5, z; boolean bool2 = !((x > 4) || (y <= 6)) == ((y <= 4) && (x > 6)); if ( x++ >= 4 || --y >= 3 ) z = x++ + --y; else z = ++x + y--; int a = 7, b = 5, c; boolean bool1 = !(b > 4) && (a <= 6) && (a <= 4) || !(b > 6); if ( ++a >= 4 && b-- >= 3 ) c = a++ + --b; else c = ++a + b--; bool1 = a = b = c = bool2 = x = y = z = 4 10) Given the following definition of class Thing2, what is the output of the Java application Test10? class Thing2 { private int count; public Thing2( int count ) { this.count = count; } public int getCount() { return this.count; } public void setCount( int count ) { this.count = count; } public String toString() { String s = " "; switch( this.count ) { case 1: s = s + "first "; case 2: s = s + "mid "; break; case 3: s = s + "last "; break; default: s = s + "rest "; break; } return s; } public void swap1( Thing2 t2 ) { int temp; temp = this.getCount(); this.setCount( t2.getCount() ); t2.setCount( temp ); } public void swap2( Thing2 t2 ) { Thing2 temp; Thing2 t1 = this; temp = t1; t1 = t2; t2 = temp; } } public class Test10 { public static void main( String[] args ) { Thing2 first = new Thing2( 1 ); Thing2 second = new Thing2( 2 ); Thing2 temp = second; second = first; first = temp; System.out.println( first.toString() ); System.out.println( second.toString() ); Thing2 third = new Thing2( 3 ); Thing2 fourth = new Thing2( 4 ); third.swap1( fourth ); System.out.println( third.toString() ); System.out.println( fourth.toString() ); second.setCount( fourth.getCount() ); third = first; System.out.println( third == first ); System.out.println( fourth == second ); System.out.println( first.toString().equals( third.toString() ) ); System.out.println( second.toString().equals( fourth.toString() ) ); System.out.println( first.toString() ); System.out.println( second.toString() ); System.out.println( third.toString() ); System.out.println( fourth.toString() ); first = new Thing2( 1 ); second = new Thing2( 2 ); first.swap2( second ); System.out.println( first.toString() ); System.out.println( second.toString() ); } } Output ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ ____________ 5 11) Given the following partial class definition fill in the body of the constructors using the supplied comments as a guide. public class Foo2 extends Foo1 { private Fubar var2; private boolean var3; public Foo2() { // Call same class ctor passing in 420 for var1, ___________________________________ // a new Fubar object invoking its no-arg ctor for // var2, and true for var3. } // Assume a no-arg ctor for Fubar is defined. public Foo2( int var1, Fubar var2, boolean var3 ) { ___________________________________ // Explicitly invoke super class (Foo1) constructor // passing the parameter var1. Assume ctor exists. ___________________________________ // Initialize the Fubar instance variable by invoking // the copy ctor for Fubar with parameter var2. // Assume a copy ctor for Fubar is defined. ___________________________________ // Initialize the boolean instance variable to the } // parameter var3. } Assuming class Foo1 has only one constructor, and based on the comments and your code above, write the full constructor that must be in class Foo1 and fill in the type for var. public class Foo1 { private _____________ var; } 12) Assuming class Foo1 has its one and only constructor correctly defined above, write the code the Java compiler will automatically insert in the class definition below. public class Foo3 extends Foo1 { } Will this code for class Foo3 compile? Why or why not? 6 13) Given the following definitions: And the following variable definitions: Thing1 thing1; Thing2 thing2; Doable doable; Indicate which are valid Java statements. Consider each statement executed sequentially in the order it appears. A) Valid Java statement – No Compiler Error B) Invalid Java statement – Compiler Error public interface Doable { public abstract void doit(); } public class Thing1 implements Doable { private static final String SPEAK = "Me"; public Thing1() { // ctor initialization here } public String speak() { return SPEAK; } public void doit() { // Thing1 does its thing } } public class Thing2 implements Doable { public static final String SPEAK = "No, Me"; public Thing2() { // ctor initialization here } public String speak( String s ) { return SPEAK + s; } public void doit() { // Thing2 does its thing } } Hint: What does the compiler know about any reference variable at compile time (vs. run time)? thing1 = new Thing1(); _______ thing1.speak(); _______ thing1.doit(); _______ thing1.speak( " Mine" ); _______ String s1 = Thing1.SPEAK; _______ thing2 = new Thing2(); _______ thing2.speak(); _______ thing2.doit(); _______ thing2.speak( " Mine" ); _______ String s2 = Thing2.SPEAK; _______ doable = new Thing1(); _______ doable.speak(); _______ doable.doit(); _______ doable = thing2; _______ doable.speak( " Mine" ); _______ doable.doit(); _______ thing1 = thing2; _______ thing1 = doable; _______ doable = new Doable(); _______ 9 Output this.a = ________ Test16.b = ________ this.c = ________ c = ________ b = ________ a = ________ this.a = ________ Test16.b = ________ this.c = ________ x = ________ a = ________ b = ________ c = ________ result = ________ this.a = ________ Test16.b = ________ this.c = ________ x = ________ a = ________ b = ________ c = ________ Use the letters below to identify various program parts. A) instance variable F) formal parameter B) static method G) constructor C) class definition (type) H) instance method D) local variable I) static variable E) actual argument _____ method1() on line 15 _____ b on line 18 _____ Test16() on line 11 _____ a on line 3 _____ b + c on line 27 _____ a on line 11 _____ main() on line 6 _____ ref on line 8 _____ Test16 on line 1 _____ b on line 4 16) What output is produced by the following program? 1 public class Test16 2 { 3 private int a; 4 private static int b = 3; 5 private int c; 6 public static void main( String[] args ) 7 { 8 Test16 ref = new Test16( 4 ); 9 ref.method1( ref.a ); 10 } 11 public Test16( int a ) 12 { 13 this.a = a; 14 } 15 public void method1( int x ) 16 { 17 int c = x--; 18 int b; 19 b = a + 2; 20 a = c + 3; 21 System.out.println( "this.a = " + this.a ); 22 System.out.println( "Test16.b = " + Test16.b ); 23 System.out.println( "this.c = " + this.c ); 24 System.out.println( "c = " + c ); 25 System.out.println( "b = " + b ); 26 System.out.println( "a = " + a ); 27 System.out.println( "result = " + method2( b + c ) ); 28 System.out.println( "this.a = " + this.a ); 29 System.out.println( "Test16.b = " + Test16.b ); 30 System.out.println( "this.c = " + this.c ); 31 System.out.println( "x = " + x ); 32 System.out.println( "a = " + a ); 33 System.out.println( "b = " + b ); 34 System.out.println( "c = " + c ); 35 } 36 private int method2( int x ) 37 { 38 int b = x; 39 int c = this.c + Test16.b; 40 x = a = b + c; 41 System.out.println( "this.a = " + this.a ); 42 System.out.println( "Test16.b = " + Test16.b ); 43 System.out.println( "this.c = " + this.c ); 44 System.out.println( "x = " + x ); 45 System.out.println( "a = " + a ); 46 System.out.println( "b = " + b ); 47 System.out.println( "c = " + c ); 48 Test16.b = b + 2; 49 this.c = a + c; 50 return x + 5; 51 } 52 } 10 Given the following class definitions for class Foo, class Fubar, and class FubarTest: public class Fubar { public Fubar( int x, int y ) { System.out.println( "Fubar ctor #1" ); } public Fubar() { this( 10, 20 ); System.out.println( "Fubar ctor #2" ); } public String toString() { System.out.println( "Fubar.toString" ); return "Fubar.toString"; } } public class Foo extends Fubar { public Foo( int x, int y, int z ) { this( x, y ); System.out.println( "Foo ctor #1" ); } public Foo( int x, int y ) { super(); System.out.println( "Foo ctor #2" ); } public Foo() { this( 4, 2, 0 ); System.out.println( "Foo ctor #3" ); } public String toString() { System.out.println( "Foo.toString" + " + " + super.toString() ); return "Foo.toString"; } } public class FubarTest { public static void main( String[] args ) { Fubar ref = new Foo(); System.out.println( "-----" ); System.out.println( ref.toString() ); } } 17) What is the output when we run FubarTest as in java FubarTest 11 Scratch Paper
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved