32
Java

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

Embed Size (px)

Citation preview

Page 1: 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

Java

Page 2: 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

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.

Page 3: 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

public int addMeeple(int x, int y) { int numMeeples = 1; numMeeples = askUser(numMeeples); return numMeeples; }

Is numMeeples in or out of scope?

Page 4: 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

public boolean hammer(double force) { if (force >= NEEDED_FORCE) { int hammerTime = hammerGoHammer(force); } return hammerTime; }

Is hammerTime in or out of scope?

Page 5: 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

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?

Page 6: 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

True or False

A child can access a field in the parent with access type protected.

Page 7: 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

Public: Full access. Protected: Class, Packages, Subclasses (like child) access. No Modifier: Class, and Packages access. Private: Class Only access.

Page 8: 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

int x = 5 % 2; x + 3; x = x * 3;

What would be the value in x after running this code?

Page 9: 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

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);

Page 10: 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

Choose the correct constructor declaration for the following class:

public class SocketWrench {

A.) SocketWrench() { B.) public class SocketWrench() { C.) public SocketWrench() {

Page 11: 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

Given class: public class SocketWrench() {

The constructor would be public SocketWrench() {

public class SocketWrench() { public SocketWrench() {...} }

Page 12: 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

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'

Page 13: 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

Evaluate the following Java expression and provide the result:false && (5 / 0 == 1)

A.) java.lang.ArithmeticException: / by zero B.) False C.) 5

Page 14: 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

Evaluate the following and provide Java's result:4 + "/" + 2

A.) “4/2” B.) 2 C.) “2”

Page 15: 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

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();

Page 16: 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

Specify what characters are necessary to begin a multi-line comment ignored by Javadoc.

A.) /** B.) /* C.) /* */

Page 17: 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

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

Page 18: 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

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

Page 19: 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

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

Page 20: 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

Is the following a valid name for a variable in Java? theIncrediblyEdiblySpiderManWithTheVeryLongNameOfMoreTha

n50Characters

A.) Yes B.) No

How many characters is the limit a variable can have in Java?

A.) 128 B.) 256 C.) No Limit

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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()

Page 24: 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

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>;

Page 25: 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

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.

Page 26: 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

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

Page 27: 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

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 3

2.5 5.7 7.5

0 1 2 3

2.5 5.7 7.5

Page 28: 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

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

Page 29: 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

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.

Page 30: 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

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.

Page 31: 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

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?

Page 32: 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

The test will probably consist of the following:

Multiple Choice Debug Code (What would cause X code not to run.) Code Writing Trick Questions

This was based off of my test in this class, but expect different, take from this the concepts.