Transcript CECS 220

Java
 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 any questions during the examples let me know.
 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.
 Public: Full access.
 Protected: Class, Packages, Subclasses (like child) access.
 No Modifier: Class, and Packages access.
 Private: Class Only access.
 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() {
 Given class:
 public class SocketWrench() {
 The constructor would be public SocketWrench() {
 public class SocketWrench() {
 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>;
 Below is sample code from a class that inherits from the JFrame class. What is the
purpose of the line super("Water District");
 public class WaterDistrictMapper extends JFrame
 {




public WaterDistrictMapper()
{
super("Water District");
}
 }
 A.) This line calls the constructor in the JFrame class with a String parameter to
initialize the JFrame fields.
 B.) This line calls the constructor in the WaterDistrictMapper class with a String
parameter to initialize the Jframe fields.
 C.) There is no output so it has no purpose.
 Class Tree contains one function diameter:
 public class Tree
 {public int diameter(int r){return r * 2;}}
 Class Redwood contains one function age:
 public class Redwood extends Tree
 {private int age; public void age(){age++;}}
 An object is created as follows: Redwood r = new Redwood();
 What output does this line of code give: r.diameter(2);
 A.) 2
B.) 3
C.) 4
 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?
}
 The test may consist of the following:
 Multiple Choice
 Debug Code (What would cause X code not to run.)
 Code Writing
 Be familiar with terminology presented in lectures 2 - 7 (e.g. buffered
i/o, primitives, deprecated services, recursion, etc.)
 Know what the distinct uses of the "this" keyword are.
 Be familiar with the constructs that are executed when an object/class
is initialized/invoked (i.e. initializers and constructors)
 Know the difference between comparing string references to string
values
 Pass-by-value in java: in the case of primitive types, the values are
copied to the stack; whereas, object types have their references copied
not the objects to which they refer