







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Main points of this past exam are: Code Segments, Operator Precedence, Denote Inheritance, Draw Stack Frames, Java Application, Class Foo1, Java Compiler
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!








On special offer
a =
b =
c =
x =
y =
z =
public static int sumLessThanVal( int[] numbers, int val ) { int sum = ____________;
for ( int i = _____________; ______________________________________________________; __________ ) { if ( ______________________________________________________ ) { _______________________________________________________ ; } } return ____________; }
class Thing { private int count;
public Thing1( int count ) { this.count = count; }
public int getCount() { return this.count; }
public void setCount( int count ) { this.count = count; }
public String toString() { if ( this.count == 5 ) return "five"; else if ( this.count == 6 ) return "six"; else if ( this.count == 7 ) return "seven"; else return "need more"; }
public void swap1( Thing1 t2 ) { Thing1 temp; Thing1 t1 = this;
temp = t1; t1 = t2; t2 = temp; }
public void swap2( Thing1 t2 ) { int temp;
temp = this.getCount(); this.setCount( t2.getCount() ); t2.setCount( temp ); } }
public class Question { public static void main( String[] args ) { Thing1 first = new Thing1( 7 ); Thing1 second = new Thing1( 5 );
Thing1 temp = first; first = second; second = temp;
System.out.println( first.toString() ); System.out.println( second.toString() );
Thing1 third = new Thing1( 5 ); Thing1 fourth = new Thing1( 4 );;
third.swap2( fourth );
System.out.println( third.toString() ); System.out.println( fourth.toString() );
first.setCount( third.getCount() ); fourth = second;
System.out.println( first == third ); System.out.println( second == fourth ); 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 Thing1( 6 ); second = new Thing1( 4 );
first.swap1( second );
System.out.println( first.toString() ); System.out.println( second.toString() ); } }
Puppy puppy; Kitty kitty; MyPet pet;
kitty = new Kitty(); _______
puppy = new Puppy(); _______
pet = kitty; _______
pet.speak(); _______
pet.wag(); _______
pet.sleep( 3000 ); _______
kitty = pet; _______
pet = new MyPet(); _______
pet = puppy; _______
pet.speak(); _______
( (Puppy) pet).wag(); _______
( (Puppy) pet).sleep( 3000 ); _______
puppy = pet; _______
puppy = kitty; _______
kitty.wag(); _______
public abstract class MyPet { public abstract String speak(); }
public class Puppy extends MyPet { private static final String PUPPY_SPEAK = "Bark";
public Puppy() { // ctor initialization here }
public String speak() { return PUPPY_SPEAK; }
public void sleep( int time ) { // puppy sleeps for time seconds } }
public class Kitty extends MyPet { private static final String KITTY_SPEAK = "Meow";
public Kitty() { // ctor initialization here }
public String speak() { return KITTY_SPEAK; }
public void wag() { // kitty wags its tail } }
abstract class Animal { private String name; public Animal() { this( "Animal" ); } public Animal( String name ) { this.name = name; } public String toString() { return name; } public abstract String speak(); }
class Cat extends Animal { public Cat() {} public Cat( String name ) { super( name + " Cat" ); } public String speak() { return "Meow"; } }
class Tiger extends Cat { public Tiger() { this( "Ko Ko" ); }; public Tiger( String name ) { super( name + " Tiger" ); } public String speak( String name ) { return name + " Roar"; } }
class BigTiger extends Tiger { public BigTiger() { super( "Anu Tiger" ); } public BigTiger( String name ) { super( name ); } public String speak() { return super.speak( "Anu" ); } }
class Lion extends Cat { public String speak() { return "Heather Lion " + super.speak(); } public String softer() { return "Bruce Lion " + super.speak(); } }
public class Test14 { public static void main( String[] args ) {
Animal a;
a = new Lion(); System.out.println( a + " says " + a.speak() );
a = new BigTiger( "Ankur" ); System.out.println( a + " says " + a.speak() );
a = new Cat( "Brina" ); System.out.println( a + " says " + a.speak() );
a = new Tiger(); System.out.println( a + " says " + a.speak() );
a = new Lion(); System.out.println( a + " says " + ((Lion) a).softer() );
} }
1 public class Test 2 { 3 private static int a; 4 private int b; 5 private int c;
6 public static void main( String[] args ) 7 { 8 Test16 ref = new Test16( 5 );
9 ref.method1( ref.c ); 10 }
11 public Test16( int c ) 12 { 13 this.c = c; 14 }
15 public void method1( int x ) 16 { 17 int c = ++x; 18 int b;
19 b = c + 3; 20 a = b + 2;
21 System.out.println( "Test16.a = " + Test16.a ); 22 System.out.println( "this.b = " + this.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( c + b ) ); 28 System.out.println( "Test16.a = " + Test16.a ); 29 System.out.println( "this.b = " + this.b ); 30 System.out.println( "this.c = " + this.c ); 31 System.out.println( "a = " + a ); 32 System.out.println( "b = " + b ); 33 System.out.println( "c = " + c ); 34 System.out.println( "x = " + x ); 35 }
36 private int method2( int x ) 37 { 38 int a = x; 39 int c = this.c + Test16.a;
40 x = b = a + c;
41 System.out.println( "Test16.a = " + Test16.a ); 42 System.out.println( "this.b = " + this.b ); 43 System.out.println( "this.c = " + this.c ); 44 System.out.println( "a = " + a ); 45 System.out.println( "b = " + b ); 46 System.out.println( "c = " + c );
47 Test16.a = a + 2; 48 this.b = b + c;
49 return x + 5; 50 } 51 }
public class Foo { public Foo( int x, int y ) { this(); System.out.println( "Foo ctor #1" ); }
public Foo() { System.out.println( "Foo ctor #2" ); }
public String toString() { System.out.println( "Foo.toString" ); return "Foo.toString"; } }
public class Fubar1 extends Foo { public Fubar1( int x, int y, int z ) { super( x, y ); System.out.println( "Fubar ctor #1" ); }
public Fubar1( int x, int y ) { this( x, y, -99 ); System.out.println( "Fubar ctor #2" ); }
public String toString() { System.out.println( "Fubar.toString" ); return super.toString() + " + " + "Fubar.toString"; } }
public class FubarTest { public static void main( String[] args ) { Foo ref = new Fubar1( 25, 151 );
System.out.println( "-----" );
System.out.println( ref.toString() ); } }