Docsity

While Loop - Computer Science - Quiz, Exercises of Computer Science

Toggle sidebar
bg1
Signature _____________________
CSE 11
Name ________________________
Quiz 4
cs11f ____
Fall 2012
Student ID ____________________
This quiz is to be taken by yourself with closed books, closed notes, no electronic devices.
What is the output produced by the following program? (Hint: draw stack frames)
public class Swap
{
private int a;
public Swap( int a )
{
this.a = a;
}
public void swap( int a, int b )
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
public void swap( Swap ref )
{
int tmp;
tmp = this.a;
this.a = ref.a;
ref.a = tmp;
}
public static void swap( Swap ref1, Swap ref2 )
{
Swap tmp;
tmp = ref1;
ref1 = ref2;
ref2 = tmp;
}
public static void main( String[] args )
{
int a = 11; Swap ref1;
int b = 22; Swap ref2;
ref1 = new Swap(9);
ref2 = new Swap(3);
Swap.swap( ref1, ref2 );
System.out.println( ref1.a );
System.out.println( ref2.a );
ref1 = new Swap(9);
ref2 = new Swap(3);
ref1.swap( a, b );
System.out.println( a );
System.out.println( b );
ref1 = new Swap(9);
ref2 = new Swap(3);
ref1.swap( ref2 );
System.out.println( ref1.a );
System.out.println( ref2.a );
}
}
Output
The different swap() method definitions have the same name but
differ in their formal parameters. This is an example of method
_______________________
pf2

Partial preview of the text

Download While Loop - Computer Science - Quiz and more Exercises Computer Science in PDF only on Docsity!

Signature _____________________ CSE 11 Name ________________________

Quiz 4

cs11f ____ Fall 2012 Student ID ____________________

This quiz is to be taken by yourself with closed books, closed notes, no electronic devices.

What is the output produced by the following program? (Hint: draw stack frames)

public class Swap{ private int a; public Swap( int a ){ }this.a = a; public void swap( int a, int b ){ int tmp; tmp = a;a = b; }b = tmp; public void swap( Swap ref ){ int tmp; tmp = this.a;this.a = ref.a; }ref.a = tmp; public static void swap( Swap ref1, Swap ref2 ){ Swap tmp; tmp = ref1;ref1 = ref2; }ref2 = tmp; public static void main( String[] args ){ int a = 11;int b = 22; Swap ref1;Swap ref2; ref1 = new Swap(9);ref2 = new Swap(3); Swap.swap( ref1, ref2 );System.out.println( ref1.a ); System.out.println( ref2.a ); ref1 = new Swap(9);ref2 = new Swap(3); ref1.swap( a, b );System.out.println( a ); System.out.println( b ); ref1 = new Swap(9);ref2 = new Swap(3); ref1.swap( ref2 );System.out.println( ref1.a ); }System.out.println( ref2.a ); }

Output

The different swap() method definitions have the same name but

differ in their formal parameters. This is an example of method

_______________________

What is the initial value of each array element in the following arrays?

Boolean[] b = new Boolean[3]; __________ double[] d = new double[4]; __________ int[] a = new int[5]; __________ boolean[] c = new boolean[7]; __________

Given the following array declaration:

int[] a = { 1, 1, 2, 3, 5, 8, ... }; // You do not know how many values are in the initializer list

Fill in the blanks to print out each element:

for ( int i = ___________ ; i < ___________ ; ___________ ) System.out.println( ___________ );

Now do the same using a foreach (enhanced for) loop:

___________ ( int i ___________ ___________ ) System.out.println( ___________ );

Now do the same using a while loop (any loop variable change do on a separate line):

_______________

while{ ( ___________________ )


} _______________________________________________

Now do the same using a do-while loop (you can assume there is at least one element in the array):

_______________ (any loop variable change do on a separate line)

_______________{

_______________________________________________

_______________________________________________

} _______________________________________________ ;

In general, if you override the equals() method you should also override the ______________________ method.

Write the code to check for exact type equivalence between two objects referenced by variables o1 and o2:

if ( _________________________________________________________________________________ )

System.out.println( "o1 and o2 reference the exact same type of object" );

else

System.out.println( "o1 and o2 reference different types of objects" );

Which is the only Java data structure than can hold primitive data types (like int) while all other standard Java

data structures can only hold an object type? ____________________________________________________

What is Rick's favorite Java operator? _________________________________________________________