Midterm Exam for Introduction to Computer Science: Java B | CSE 8B, Exams of Computer Science

Material Type: Exam; Class: Intro/Computer Science: Java B; Subject: Computer Science & Engineering; University: University of California - San Diego; Term: Winter 2004;

Typology: Exams

Pre 2010

Uploaded on 03/28/2010

koofers-user-sly
koofers-user-sly ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Page 1 (15) _____________ CSE 8B Name ________________________
Page 2 (41) _____________ Midterm
Page 3 (16) _____________ cs8f________
Page 4 (33) _____________ Winter 2004
Signature _________________________
Total (105) _____________
Student ID _____________________
This test is to be taken by yourself with closed books, closed notes, no calculators. Just your 1-sided summary sheet.
Operator Precedence Table
Operators Associativity
- (unary) ++ -- ! right to left
* / % left to right
+ - left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
= += -= *= /= right to left
Page 1) Write the code to perform the specified operations. (9 pts)
import java.util.Random;
public class M1 {
public static void main( String[] args ) {
int[][] a = new int[3][4];
int k = 0;
Random rand = new Random();
for ( int i = 0; i < a.length; ++i )
for ( int j = 0; j < a[i].length; ++j )
a[i][j] = rand.nextInt( 100 ); // Initialize the array a with random values
/*
* Write a nested for loop to print out the elements in the array a in reverse order in which
* they are stored. Don't reverse the actual values, just print the values accessing the elements
* from the last cell to the first.
*/
} // end of main()
} // end of class M1
The readLine() method returns _____________ to indicate no more input. (2 pts)
List two features of constructors that differentiate them from methods. (4 pts)
1)
2)
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam for Introduction to Computer Science: Java B | CSE 8B and more Exams Computer Science in PDF only on Docsity!

Page 1 (15) _____________ CSE 8B Name ________________________

Page 2 (41) _____________ Midterm

Page 3 (16) _____________ cs8f________

Page 4 (33) _____________ Winter 2004

Signature _________________________

Total (105) _____________

Student ID _____________________

This test is to be taken by yourself with closed books, closed notes, no calculators. Just your 1-sided summary sheet.

Operator Precedence Table

Operators Associativity

- (unary) ++ --! right to left

* / % left to right

+ - left to right

< <= > >= left to right

== != left to right

&& left to right

|| left to right

= += -= *= /= right to left

Page 1) Write the code to perform the specified operations. (9 pts)

import java.util.Random;

public class M1 {

public static void main( String[] args ) {

int[][] a = new int[3][4];

int k = 0;

Random rand = new Random();

for ( int i = 0; i < a.length; ++i )

for ( int j = 0; j < a[i].length; ++j )

a[i][j] = rand.nextInt( 100 ); // Initialize the array a with random values

* Write a nested for loop to print out the elements in the array a in reverse order in which

* they are stored. Don't reverse the actual values, just print the values accessing the elements

* from the last cell to the first.

} // end of main()

} // end of class M

The readLine() method returns _____________ to indicate no more input. (2 pts)

List two features of constructors that differentiate them from methods. (4 pts)

Page 2) Consider the following recursive function.

void Mystery( int n )

int d;

if ( n > 0 )

d = n % 10;

n = n / 10;

Mystery( n );

System.out.print( d + " " ); // Output the value of d followed by a space

Which of the following is output as a result of the call Mystery( 1027 )? Circle the correct Letter. (5 pts)

A. 7 9 9 10 D. 10 27 72 01

B. 1 0 2 7 E. 1 1 3 10

C. 7 2 0 1 F. 10

Fill in the blanks. (24 pts โ€“ 2 pts each)

Primitive data types can be directly inserted into ______________ while all other data structures require objects.

The keyword _________________ specifies that a variable is not modifiable after it is initialized.

In order to insert a primitive int value into an ArrayList, you must first wrap/box the int value into a(n) __________________ object. The ListIterator method used to check if there are any more elements in this iteration is ________________.

The _____________ operator dynamically allocates memory for an object of a specified type in the ________________ and returns a (what area of the Java Runtime Environment) ____________________ to that object. ____________________ variables are located in these objects.

_____________ is a hidden parameter passed to every _________________________ method and ___________________________.

Members of a class specified as _____________________ are accessible only to methods of the class.

Which method defined in class M2 below will be matched with the following method invocations? (12 pts โ€“ 2 pts each) A) Method labeled with / 1 / B) Method labeled with / 2 / C) Both methods โ€“ ambiguous D) Neither methods โ€“ no match

public class M { / 1 / public int aMethod( float a, long b ) { /* method implementation here / } / 2 / public int aMethod( int a, double b ) { / method implementation here */ } }

_________ aMethod( 420, 1.23 ); _________ aMethod( (short) 420, (long) 1.23 );

_________ aMethod( 4.20, (long) 23 ); _________ aMethod( 23, 420 );

_________ aMethod( (long) 42, 123 ); _________ aMethod( 1.23, 420 );

Write the Letter (A-D) corresponding to the correct answer on the line in front of every method call.

Page 4) Indicate whether each of the following parts of a Java program is (A-H) and where in the Java Runtime Environment this lives (1-3) (1 pt each)

A) Class (static) variable 1) Method Area / Class Area B) Instance variable 2) Object in the Heap C) Local variable 3) Stack Frame in the Runtime Stack D) Parameter E) Constructor F) Static method G) Instance method H) Class definition Java program part Java Runtime area (Answer A-H in this column) (Answer 1-3 in this column)

public class M4 { ________ M public char actor; ________ actor ________ private static int ellected; ________ ellected ________ public static int getEllected() { return ellected; } ________ getEllected ________ } public class SomeOtherClass { ________ SomeOtherClass private double digit; ________ digit ________ public static void main( String[] args ) { ________ main ________ ________ args ________ M4 ref; ________ ref ________ ref = new M4(); (where ref is pointing) ________ ... // *** HERE *** } public int foo( boolean test ) { ... } ________ foo ________ ________ test ________ }

Write a statement that could appear in the code above at the line marked //*** HERE *** that

a) assigns the return value of calling foo() passing the expression "5 less than or equal to 9" to actor in the object referenced by ref. Hint: Be aware of the types. (6 pts)

b) assigns the value in the private variable ellected in class M4 to digit. (6 pts)

Scratch Paper