1 / 30

Qwizdom : Basic Java review

Qwizdom : Basic Java review. Chris Loftus. Which of the following signatures are valid for the main() method entry point of an application?. public static void main() public static void main(String arg []) public void main(String [] arg ) public static void main(String [] args )

ajay
Download Presentation

Qwizdom : Basic Java review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Qwizdom: Basic Java review Chris Loftus

  2. Which of the following signatures are valid for the main() method entry point of an application? • public static void main() • public static void main(String arg[]) • public void main(String [] arg) • public static void main(String [] args) • public static int main(String [] arg)

  3. Study the following class diagram.The multiplicity constraint 0..* on the association between MUDGame and Treasure means (choose one): Player --------------------- -String name -intpowerPoints -Treasure[] booty -Image image --------------------- +void takeTurn() MUDGame --------------------- -Player[] players -Treasure[] treasures --------------------- +void run() 0..* • A MUDGame class is connected to many Treasure classes • An object of class Treasure may be connected to zero or many MUDGame objects • An object of class MUDGame may be connected to zero or many Treasure objects • A Treasure class is connected to many MUDGame classes 0..* Treasure --------------------- -String specialPower -Image image --------------------- +void applyPower() 0..* 1..1 Image --------------------- -String filename 1..1

  4. Consider the following line of code:int x[] = new int[25];After execution, which statement or statements is/are true? • x[24] is 0 • x[24] is undefined • x[25] is 0 • x[0] is null • x.length is 25

  5. Consider the following application: 1. class HolderApp { 2. public static void main(String args[]){ 3. Holder h = new Holder(); 4. h.held = 100; 5. h.bump(h); 6. System.out.println(h.held); 7. } 8. } 9. class Holder {10. public int held;11. public void bump(Holder theHolder){12. theHolder.held++;13. }14. }What value is printed out at line 6? • 0 • 1 • 100 • 101

  6. Study the following class diagram.The multiplicity constraint 1..1 on the association between Treasure and Image means (choose one): Player --------------------- -String name -intpowerPoints -Treasure[] booty -Image image --------------------- +void takeTurn() MUDGame --------------------- -Player[] players -Treasure[] treasures --------------------- +void run() 0..* • An object of class Treasure must be connected to one Image class • An object of class Treasure may be connected to one Image object • An object of class Treasure must be connected to one object of class Image 0..* Treasure --------------------- -String specialPower -Image image --------------------- +void applyPower() 0..* 1..1 Image --------------------- -String filename 1..1

  7. Study the following class diagram.The constraint “-” before String specialPowermeans (choose one): Treasure --------------------- -String specialPower -Image image --------------------- +void applyPower() • The attribute specialPower is private to the Treasure class • The attribute specialPower is public to all classes • The attribute specialPower is protected within the Treasure class

  8. Consider the following application: 1. class DecrementerApp{ 2. public static void main(String args[]){ 3. double d = 12.3; 4. Decrementerdec = new Decrementer(); 5. dec.decrement(d); 6. System.out.println(d); 7. } 8. } 9. class Decrementer{10. public void decrement(double decMe){11. decMe = decMe – 1;12. }13. }What value is printed out at line 6? • 0.0 • -1.0 • 12.3 • 11.3

  9. After execution of the code fragment below, what are the values of the variables x, a, and b?1. int x, a = 6, b = 7;2. x = a++ + b++; • x = 15, a = 7, b = 8 • x = 15, a = 6, b = 7 • x = 13, a = 7, b = 8 • x = 13, a = 6, b = 7

  10. Quiz: How many times does this loop execute? int n = 10; for (int i=0; i<=n; i++){ // statements here } • 10 times • Only once since i is only incremented once • 11 times • 0 times, since i is given the value 0 • It never stops

  11. Quiz: How many times does this loop execute? int n = 10; for (int i=0; i<n; i--){ // statements here } • 10 times • Only once since i is only incremented once • 11 times • 0 times, since i is given the value 0 • It only stops when i has value -2147483648

  12. Study the following code fragment:1. String [] names = { “Fred”, “Jim”, “Sheila” };2. for (inti=0; i <= names.length; i++){3. System.out.print(names[i] + “ – “);4. } What output results when this code fragment is run? (Choose one) • An ArrayIndexOutOfBoundsException is thrown at line 3 • Fred – Jim – Sheila – ArrayIndexOutOfBoundsException thrown at line 3 • Fred – Jim – Sheila -

  13. What results from the following fragment of code?1. int x = 1;2. String [] names = { “Fred”, “Jim”, “Sheila” };3. names[--x] += “.”;4. for (inti=0; i < names.length; i++){5. System.out.println(names[i]);6. }Choose one answer. • The output includes Fred. with the trailing full stop • The output includes Jim. with the trailing full stop • The output includes Sheila. with the trailing full stop • None of the outputs shows a trailing full stop • An ArrayIndexOutOfBoundsException is thrown

  14. Lab: Basic Java all tasks

  15. Study the following class diagram.What is the meaning of the relationship between Employee and Manager? (choose one): Employee • Employee extends Manager • Employee implements Manager • Manager implements Employee • Manager extends Employee Manager

  16. Which one statement is true about the following code fragment?1. int j = 2; 2. switch (j) { 3. case 2: 4. System.out.println(“value is two”); 5. case 3: 6. System.out.println(“value is three”); 7. break; 8. default: 9. System.out.println(“value is “ + j);10. break;11. } • The code will not compile because case 2 is missing a break statement • The output would be only the text valueis two • The output would be the text value is two followed by the text value is three • The output would be the text value is two, followed by the text value is three, followed by the text value is 2

  17. Quiz: What is printed if the user types in “one”? String answer = in.next(); switch (answer.charAt(0)){ case ‘o’: System.out.println(“Typed one”); case ‘t’: System.out.println(“Typed two or three”); break; default: System.out.println(“Something else”); } • Typed one • Typed two or three • Something else • None of the above

  18. Study the following class diagram.The constraint “+” before void applyPower() means (choose one): Treasure --------------------- -String specialPower -Image image --------------------- +void applyPower() • The attribute applyPower is public to all classes • The method applyPower is public to all classes • The method applyPower is protected within the Treasure class • The attribute applyPower is protected within the Treasure class

  19. Study the following class diagram.What is the meaning of the relationship between Employee and Manager? (choose one): <<interface>> Employee • Employee extends Manager • Employee implements Manager • Manager implements Employee • Manager extends Employee Manager

  20. Quiz: When should we use a collection such as ArrayList? (Choose the most accurate answer) • When we have a fixed number of data items to store in an object • When we have a variable and unknown number of items to store in an object • When we want to make our code more flexible

  21. Quiz: arrayList.add(“value”) inserts “value” at the start of arrayList. TRUE or FALSE?

  22. Consider this class:1. public class Test1 { 2. public float aMethod(float a, float b) { 3. return 0.0F; 4. } 5. 6. }Which of the following methods would be legal if added (individually) at line 5? • public intaMethod(int a, int b) {return 0;} • public float aMethod(float a, float b) {return 0.0F;} • public float aMethod(float a, float b, int c) {return 0.0F;} • public float aMethod(float c, float d) {return 0.0F;} • private float aMethod(int a, int b, int c) {return 0.0F;}

  23. Consider the following fragment of code:1. try { 2. runMethod(); 3. } 4. catch(IOExceptionioe){ 5. System.out.println(“IOException thrown”); 6. } 7. catch(FileNotFoundExceptionfnfe){ 8. System.out.println(“FileNotFoundException thrown”); 9. }10. catch(Exception e){11. System.out.println(“Exception thrown”); 12. }13. finally {14. System.out.println(“In finally”); 15. }What output results when runMethod completes without throwing an exception? (Choose one) • The string In finally is displayed • Nothing is displayed

  24. Consider the following fragment of code:1. try { 2. runMethod(); 3. } 4. catch(IOExceptionioe){ 5. System.out.println(“IOException thrown”); 6. } 7. catch(FileNotFoundExceptionfnfe){ 8. System.out.println(“FileNotFoundException thrown”); 9. }10. catch(Exception e){11. System.out.println(“Exception thrown”); 12. }13. finally {14. System.out.println(“In finally”); 15. }What output results when runMethod throws ClassCastException? (Choose one) • The string Exception thrown followed by In finally are displayed • The string Exception thrown is display • All the above messages from lines 5, 8,11 and 14 are displayed

  25. Consider the following fragment of code:1. try { 2. runMethod(); 3. } 4. catch(Exception ioe){ 5. System.out.println(“Exception thrown”); 6. } 7. catch(FileNotFoundExceptionfnfe){ 8. System.out.println(“FileNotFoundException thrown”); 9. }10. catch(IOException e){11. System.out.println(“IOException thrown”); 12. }13. finally {14. System.out.println(“In finally”); 15. }What output results when runMethod throws IOException? (Choose one) • The string IOException thrown followed by In finally are displayed • The string Exception thrown followed by In finally are displayed • The string IOException thrown is display • All the above messages from lines 5, 8,11 and 14 are displayed

  26. Consider this method:1. public void readKeyboard(){2. Scanner in = new Scanner(System.in);3. System.out.print("enter license: ");4. licenseNumber = in.next();5. }When called, this method will do the following (choose the most accurate)? • Create a Scanner object that will read text from a file, and will on line 4 read text until a whitespace character is reached and will return that text as a String • Create a Scanner object that will read text from the standard input (probably the keyboard), and will on line 4 read text until a whitespace character is reached and will return that text as a String • Create a Scanner object that will read text from the standard input (probably the keyboard), and will on line 4 read text until a whitespace character is reached and will return that text as an int

  27. Consider the following classes, declared in separate source files:1. public class Base { 2. public void meth(inti){ 3. System.out.print(“Value is “ + i + “ - “); 4. } 1. public class Sub extends Base { 2. public void meth(int j){ 3. System.out.print(“This value is “ + j + “ - “); 4. } 5. public static void main(String args[]){ 6. Base b1 = new Base(); 7. Base b2 = new Sub(); 8. b1.meth(5); 9. b2.meth(6);10. }11. }What output results when the main method of the class Sub is run? (Choose one) • Value is 5 - Value is 6 • This value is 5 - This value is 6 • Value is 5 - This value is 6 • This value is 5 - Value is 6

  28. Quiz: What is printed if the user types in “three”? String answer = in.next(); switch (answer){ case “one”: System.out.println(“Typed one”); break; case “two”: case “three”: System.out.println(“Typed two or three”); break; default: System.out.println(“Something else”); } • Typed one • Typed two or three • Something else • None of the above

  29. for (Person p: people) { System.out.println(p); } Consider the following code and design. What will be displayed? Person ----------------------- -String name etc ----------------------- +toString(): String 0..* Application ----------------------- -people: Person[] This will not compile Zero or many Person details An incomprehensible set of characters representing objects

  30. Lab: Basics: Times and Clocks

More Related