SampleProblemsExam1

# SampleProblemsExam1 - 1/11 EE 322C Practice Problems for...

This preview shows pages 1–4. Sign up to view the full content.

1/11 EE 322C Practice Problems for First Midterm Answers are highlighted in grey. Section 1 : Expressions, What does this code do The exercises from the first and second quiz provide many practice problems that could fall in this section. Here are a few more: What is the output of the following code snippet? int a = 2, b = 5, c = 6; System.out.println(a + b + “ “ + c); System.out.println(a + “ “ + b + c); Answer : 7 6 2 56 a. There is an error in the following code, what is it? int x = 32; if (x/2) System.out.println(“x is even”); Answer : x/2 is not a Boolean so the if statement is an error. b. What (if any) is the difference in the following two implementations of the min function? public static int min( int x, int y) { return x < y ? x : y; } public static int min( int x, int y) { return x <= y ? x : y; } Answer : No difference

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2/11 Section 2 : Static vs. Non-Static a. What is the output of the java program App.java? public class X { static int gcount = 0; int value; public X() { value = ++gcount; } public String toString() { return “value: “ + value; } } public class App { public static void main(String [] args) { System.out.println(X.gcount); X foo = new X(); X bar = new X(); System.out.println(bar); System.out.println(foo); System.out.println(X.gcount); } } Answer: The best way to approach questions like this is to visualize it. Variable gcount in X is a static variable, i.e., a class variable. Variable value in X is a instance variable. Note from the figure the following: gcount is a class variable so it has space allocation in the class X gcount is accessible from within instances but in all instances (here foo aad bar) they are pointing to the gcount in class X value is an instance variable therefore it has space in each of the instances, foo and bar. The toString method is a non-static method and so belongs to the instance.
gcount = 0 X value gcount toString() foo value gcount toString() bar Now we are ready to answer the question. The first line in main: System.out.println ( X.gcount ); // print 0 to screen is accessing the gcount variable from class X and so it is valid and the vaue of gcount is initially set to 0 so that is what this line prints. The second line: X foo = new X(); creates a new instance of X called foo by invoking the constructor of X. The constructor sets the instance variable value of foo to be the value of gcount after incrementing gcount by one. gcount = 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 09/26/2009 for the course EE 322C taught by Professor Nettles during the Spring '08 term at University of Texas.

### Page1 / 11

SampleProblemsExam1 - 1/11 EE 322C Practice Problems for...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online