60 likes | 70 Views
This quiz explores key concepts in Java, including the use of "this" in class methods, constructors, arrays, abstract classes, interfaces, and more. Each question is explained briefly and code examples are provided.
E N D
Quiz 1 What is this? (explain the use of the reserved word “this” in a class method). Answer each question briefly. What is a Constructor? Under what circumstances would a Constructor method for a class execute? Can a class have more than one Constructor? Given String s = "potato"; String v = s.substring(0,3); String w = s.substring(0,3); Is v==w true or false? This is an object reference that refers to the current object for which a method was invoked. Brief answers A method invoked on object creation. Typically, a Constructor is executed when an object is created by use of the “new” operator. Yes, as long as they have different parameters. v == w is false. The substring method will create new objects each time it is invoked. Quiz 1
The following code creates a 2x2 block of buttons. Add/modify the code to create an applet with 6 buttons arranged in 3 rows and 2 columns.. import javax.swing.*; import java.awt.*; public class ButtonJComponent extends JApplet { public void init() { Container c = getContentPane(); c.setLayout(new GridLayout(2,2); JButton button1 = new JButton("one"); JButton button2 = new JButton("two"); JButton button3 = new JButton("three"); JButton button4 = new JButton("four"); } } 2. Discuss the changes necessary to make the program in 1 a stand-alone java app instead of an applet. You may discuss this in words…exact code is not necessary in all cases, but try to cover all the steps. For example: “extends JApplet” changes to “???” The init method changes to…. Must add a main method that does what? Anything else? Change body to: c.setLayout(new GridLayout(3,2); JButton button1 = new JButton("one"); JButton button2 = new JButton("two"); JButton button3 = new JButton("three"); JButton button4 = new JButton("four"); JButton button3 = new JButton("five"); JButton button4 = new JButton("six"); The following two lines are sufficient: for(int i=0; i < b.length; i++) System.out.println(b[i]); -extends JApplet changes to extends JFrame -the init method becomes a constructor ButtonJComponent() -A main method must be added which creates a ButtonJComponent object, and calls the setSize() and show() methods -a windowclosing handler should be added Quiz 2
1. What error will Java report from the following code? (That is…what’s wrong with this sequence? You do not need the actual error message.) int[] a; a[3] = 5; 2. Correct any errors in the following declaration. char[] vowel = {a, e, i, o, u}; 3. Consider int v = {23, 44, 78}; int w = v; w[2] = 100; What is the value of v[2]? 4. Write Java code to swap integer array elements a[i] and a[j]. a is a reference to an array, but the array has not been created (with the new operator for instance). The actual error reported is: variable a might not have been initialized char[] vowel = {‘a’,’b’,’c’,’d’,’e’}; v[2] has the value of 100. (w is an alias of v so the statement w[2] = 100 also changes v. Something like this: int I,J,temp; temp = a[I]; a[I] = a[J]; a[I] = temp; Quiz 3-1
Show the contents of a stack (of Strings) after the every 5 operations and at the end.Assume the stack is initially empty. Display the stack from Top to bottom. (Commands are listed left to right to conserve space. Push (“Example”) Push (“Bogus”) Push (“Sham”) Pop() Pop() Push(“Fine”) Push(“A”) Push(“Of”) Pop() Push(“Heck”) Push(“One”) Pop() Pop() Push(“Is”) Push(“List”) Pop() Push(“This”) At the end the stack will have (from top to bottom) This Is A Fine Example The list of operations is intended to be one continuous list, with line breaks. So the final answer is (from left to right): it is a Fine Example Anyway Quiz 4
Which of the following correctly describes the relation between an abstract class and an interface? An interface is another name for an abstract class. An interface is not a class, but is otherwise identical to an abstract class. Every abstract class implements an interface. An abstract class must be totally abstract, but an interface may be partially implemented. None of the above The code public class B extends A { } defines a class that inherits all the methods of A defines a class that inherits the public methods of A only will not compile because the body in empty none of the above The code public class B extends A { } defines a class B that implements A is a superclass of A is a subclass of A none of the above None of the above. All of the statements are false. None of the above. B inherits public and protected methods and if they are in the same package, B will inherit methods created with no modifier. B is a subclass of A Quiz 5
Quiz 6 • What is displayed on the console when the following code is run? • class Test • { • public static void main(String[] args) • { • try • { • System.out.println("Welcome to Java"); • int i=0; • int y=2/i; • System.out.println("Exceptions are fun"); • } • catch(RuntimeException ex) • { • System.out.println("I take Exception to that"); • } • finally • { • System.out.println("End of the block"); • } • } • } • Welcome to Java • I take Exception to that • End of the block • What is displayed on the console when the following code is run? • class Test1 • { • public static void main(String[] args) • { • try • { • System.out.println("Welcome to Java"); • int i=0; • System.out.println("Exceptions are fun"); • } • catch(RuntimeException ex) • { • System.out.println("I take Exception to that"); • } • finally • { • System.out.println("End of the block"); • } • } • } • Welcome to Java • Exceptions are fun • End of the block