CECS 220 – Final Review

Download Report

Transcript CECS 220 – Final Review

CECS 220 – Final Review
By: Joseph M. Ouellet
Question 1
•
•
•
•
•
•
public boolean hammer(double force){
if(force >= NEEDED_FORCE){
int hammerTime = hammerGoHammer(force);
}
return hammerTime;
}
•
In the line “return hammerTime;”, is hammerTime in or out of scope?
Question 2
•
Choose the correct constructor signature for the following class:
•
public class Elephant {}
•
•
•
•
A. public Elephant(){}
B. public class Elephant::Elephant(){}
C. public class Elephant(){}
D. public Elephant::Elephant(){}
Question 3
•
•
•
•
•
•
•
public class Bank {
public int amount;
•
•
•
public void deposit(Bank b, int funds){
b.amount += funds;
}
•
•
•
•
After these lines of code what would be the output?
Bank bank = new Bank(500);
deposit(bank, 100);
System.out.println(bank.amount);
public Bank(int amount){
this.amount = amount;
}
}
Question 4 - 8
•
4. Evaluate: “5”+ 2;
•
5. Multi-line comment begins with?
•
6. True or False: The following is a valid name for a variable in Java: Bang1
•
7. When using floating point numbers in an ArrayList, Java stores each floating point number
as an object, not a basic type.
•
8. What is the limiting factor in the size that an ArrayList can grow to in Java?
A.
B.
C.
D.
No factor, the ArrayList cannot grow, it has a set size.
Maximum memory allocated to Java.
Type placed in the ArrayList.
Size of data placed in ArrayList.
Question 9
•
•
•
double skill = 1.5;
skillBuilder(skill);
skill += 0.5;
•
•
•
public void skillBuilder(double skills){
skills = skills + 100;
}
•
After running the first three lines of code what is the output?
•
•
•
•
A. 102.0
B. 2.0
C. 0.5
D. 100.5
Question 10
•
How many times would the following code loop?
•
•
•
•
•
double count = 99.5;
while(count < 100.0){
System.out.println(count);
count -= 0.05;
}
•
•
•
•
A. 100 Times
B. 0 Times
C. Infinite
D. 2000 Times
Question 11
•
•
•
•
•
•
ArrayList<Float> list = new ArrayList<>();
list.add(2.5);
list.add(3.7);
for([temp] : [collection]){
// Print
}
•
•
What is inside [temp]?
What is inside [collection]?
Question 12 - 13
•
12. How do you obtain number of items in an array? Array name is arr.
•
•
•
•
A. arr.sizeof
B. arr.getSize
C. arr.length
D. arr.size
•
•
13. How do you change the value of an array element?
boolean[] arr = new boolean[5];
•
•
•
•
A. arr[] = true;
B. arr[1] = true;
C. arr[].set = true;
D. boolean[1] arr = true;
Question 14
•
•
What does the following code do?
Jbutton button = new Jbutton(“Submit”);
•
A. Nothing is done yet. No button object is created until after it has been added to a Jframe
and the Jframe is displayed.
B. Creates a new button object with the text “Submit” written across the button that is not
yet visible.
C. Creates a new button object with the text “Submit” written across the button that is
visible and ready for use.
D. Nothing is done yet. A button object is created, but it hasn’t been added to a Jframe yet,
therefore it cannot be displayed.
•
•
•
Question 15 - 17
•
•
•
•
•
15. Which method is run first within an applet? Public void [method]()
A. begin
B. run
C. init
D. append
•
•
16. Fill in the blanks.
Public [modifiers] [return type] main([args_type] args){}
•
•
17. Complete the for loop below so that is produces the sequence: 3, 5, 7, 9.
for(int i = [initial]; i <= [end]; i += [increment]){}
Question 18 - 20
•
18. You can mix layout types (BorderLayout, GridBagLayout) within a single GUI.
•
19. If a jar file is setup correctly, it can be run directly without a compiler.
•
20. The MouseListener interface contains a method called mouseEntered. When is this
method called when added as a listener?
•
•
•
•
A. When the mouse is pressed on the object it is listening to.
B. When the mouse is released on the object it is listening to.
C. When the mouse cursor moves over the border of the object it is listening to.
D. When the mouse cursor is away from the object, but is pressed.
Spotting Errors
•
What line and in which class is the exception taking place?
•
•
•
•
•
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException
at spring2011.cecs220.examples.hearts.HeartsGUI.actionPerformed(HeartsGUI.java:225)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonMode1.fireActionPerformed(DefaultButtonMode1.java:387)
Study Tips
•
•
•
•
•
•
•
•
•
•
•
How to evaluate code.
How the DrJava compiler works.
How classes work in Java.
How to find errors in code and correct them.
The syntax of Java such as various for loops, while loops, etc.
Know the order of classes and what “this” does and means inside of methods.
Know how to write all the code you done on your various assignments by hand.
Read each question carefully, if you miss a “;” or “.” somewhere you are wrong.
Expect the same format from last exam and the same material from last exam.
Make sure if you missed questions last exam, study them so they don’t get you twice.
Good luck.