CECS 220

advertisement
Midterm Test Review Session
Provided by REACH
Presented by [tutor]
 During this PowerPoint we will go over possible questions that could be
on the test.
 I assume you know some if not all the syntax.
 If you have questions, especially “basic” ones, ASK! See excerpt from a
previous class’s slideshow:
 public int addMeeple(int x, int y)
 {



int numMeeples = 1;
numMeeples = askUser(numMeeples);
return numMeeples;
 }
 Is numMeeples in or out of scope?
 public boolean hammer(double force)
 {





if (force >= NEEDED_FORCE)
{
int hammerTime = hammerGoHammer(force);
}
return hammerTime;
 }
 Is hammerTime in or out of scope?
 public int notRandom(int check)
 {



if (check == 3){return 3;}
if (check > 4){return 13;}
return 4;
 }
 If you call notRandom(2); what does it return to you?
 True or False
 A child can access a field in the parent with access type protected.
 int x = 5 % 2;
 x + 3;
 x = x * 3;
 What would be the value in x after running this code?

public class Pirate

{

public int plunder(int hours, Crew crew)

{

// Do plundering steps here.

return amount;

}

}

Object Creation Steps:

Pirate blackBeard = new Pirate("Black Beard");

Crew ruffians = new Crew(36);

Which of the following lines would make Black Beard plunder for 3 hours with 36 crew?

A.) blackBeard.plunder(3, ruffians);

B.) blackBeard.plunder(3, 36);

C.) blackBeard.plunder(3, crew);
 Choose the correct constructor declaration for the following class:
 public class SocketWrench {
 A.) SocketWrench() {
 B.) public class SocketWrench() {
 C.) public SocketWrench() {
 Evaluate the following Java expression and provide the result:
Double.MIN_VALUE + 1
 A.) No output.
 B.) 1.0
 C.) Static Error: No member of Double has name 'MIN_VALUE'
 Evaluate the following Java expression and provide the result:
false && (5 / 0 == 1)
 A.) java.lang.ArithmeticException: / by zero
 B.) False
 C.) 5
 Evaluate the following and provide Java's result:
4 + "/" + 2
 A.) “4/2”
 B.) 2
 C.) “2”
 How do you access the static method getNumBurgers after the
following statement (getNumBurgers is contained within the Burger
class):
 Burger b = new Burger(Burger.DOUBLE_STACK);
 public static int getNumBurgers()
 {return numBurgers;}
 A.) Static Error: Undefined name 'DOUBLE_STACK'
 B.) b.getNumBurgers();
 C.) Burger.getNumBurgers();
 Specify what characters are necessary to begin a multi-line comment
ignored by Javadoc.
 A.) /**
 B.) /*
 C.) /* */
 What will the result of the following code be?
 JFrame jf = null;
 System.out.println(jf.getX());
 A.) It would not compile.
 B.) java.lang.NullPointerException
 C.) null
 Is the following a valid name for a variable in Java?
 _special_character
 A.) Yes
 B.) No
 Is the following a valid name for a variable in Java?
 $vari_A1
 A.) Yes
 B.) No
 Is the following a valid name for a variable in Java?
 #include
 A.) Yes
 B.) No
 The following variable name is not allowed in Java, why?
 9pins+1
 A.) It has an operator ‘+’ in it.
 B.) It has a number ‘9’ before a character.
 C.) Error: Incomplete expression
 Is the following a valid name for a variable in Java?
 theIncrediblyEdiblySpiderManWithTheVeryLongNameOfMoreThan50Charact
ers
 A.) Yes
 B.) No
 How many characters is the limit a variable can have in Java?
 A.) 128
 B.) 256
 C.) No Limit
 What will the value of variable i be after executing these two lines of Java code?
 String s = “ABBA”;
 int i = s.indexOf('B');
 A.) 2
 B.) 3
 C.) 1
 What is the value as evaluated by Java of the following line of code?
 Math.ceil(3.2);
 A.) 3.2
 B.) 4.0
 C.) 3.0
 How do you access the static method getNumWorkers after the
following statement:
 Worker w = new Worker(“Johnny Unitas”);
 public static int getNumWorkers()
 {

return numWorkers;
 }
 A.) Worker.getNumWorkers()
 B.) w.getNumWorkers()
 C.) w.Worker.getNumWorkers()
 Before you use a class like Vector in your program you need to include
the class or package into your code. Which statement below does this
for Vectors? The header for the Java API doc for the Vector class is
below:
 java.lang.Object



java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.Vector<E>
 A.) import java.util.Vector;
 B.) import java.util.*;
 C.) import java.util.Vector<E>;
 Execute the following Java code. Then choose which answer is
represents the current state of the ArrayList list. (Ignore initial size and
capacity of array.)
 ArrayList<Double> list = new ArrayList<Double>();
 list.add(2.5); list.add(3.6); list.add(5.7); list.add(7.5); list.remove(1);
 A.)
 B.)
0
1
2
2.5
5.7
7.5
0
1
2
3
5.7
7.5
2.5
3
 What value is output in the following code snippet:
 Set<Integer> set = new HashSet<Integer>();
 set.add(1);
 set.add(3);
 set.add(3);
 set.add(7);
 System.out.println(set.size());
 A.) 4
 B.) 14
 C.) 3
 What is the purpose of the <Double> reference?
 Vector<Double> vector = new Vector<>();
 A.) It sets the type of object that may be added to the vector.
 B.) It doubles the vector size for larger storage.
 C.) It doubles the input put into the vector and turns into a double
type.
 Both the Vector and ArrayList classes represent "growable" lists of
objects. Internally these classes store objects in arrays. If 10 was used as
the initial capacity parameter, what happens when the 11th object is
added to the vector?
 A.) The vector grows to size of 11 allowing up to 12 objects.
 B.) Nothing happens because 0 – 10 allows 11 objects.
 C.) The vector doubles in size if not specified by another variable.

1 public class Time

2 {

3
public int getHour(String time)

4
{

5
int index = time.indexOf(':');

6
String hourString = time.subsring(0, index);

7
int hour = Integer.parseInt(hourString);

8
return hour;

9

10 }

Why do you get an error when you try to compile this code?
}
 Can be used two different ways: with a field:
 public class Point {


public int x = 0;
public int y = 0;






 }
 public class Point {


public int x = 0;
public int y = 0;

//constructor
public Point(int a, int b) {
x = a;
y = b;
}





//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
}
 }
Each argument to the constructor shadows one of the object's fields — inside the
constructor, x is a local copy of the constructor's first argument. To refer to the Point
field x, the constructor must use this.x.
 Can be used two different ways: …or with a constructor:

public class Rectangle {

private int x, y;


private int width, height;

public Rectangle() {

this(0, 0, 1, 1);

}

public Rectangle(int width, int height) {
this(0, 0, width, height);

}


public Rectangle(int x, int y, int width, int height) {

this.x = x;

this.y = y;

this.width = width;

this.height = height;
}


...
This class contains a set of
constructors. Each constructor
initializes some or all of the
rectangle's member variables.
The constructors provide a
default value for any variable
whose initial value is not
provided by an argument. For
example, the no-argument
constructor creates a 1x1
Rectangle at coordinates 0,0.
The two-argument constructor
calls the four-argument
constructor, passing in the
width and height but always
using the 0,0 coordinates. As
before, the compiler
determines which constructor
to call, based on the number
and the type of arguments.
 Based on previous years, the test may consist of the following:
 Multiple Choice
 Debug Code (What would cause X code not to run.)
 Code Writing
 Other areas of emphasis:
 Terminology used in lectures: e.g. buffered I/O, primitives, deprecated
services, recursion, etc.
 The constructs that are executed when an object/class is initialized/invoked
(i.e. initializers and constructors)
 The difference between comparing string references and string values.
 Pass-by-value in Java: in the case of primitive types, values are copied to the
stack; whereas, object types have their references copied, not the objects to
which they refer.
Download