Quiz 1 - PowerPoint PPT Presentation

About This Presentation
Title:

Quiz 1

Description:

Quiz 2. The following code creates a 2x2 block of buttons. ... Quiz 4 ... Quiz 5 ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 7
Provided by: robert403
Category:
Tags: quiz

less

Transcript and Presenter's Notes

Title: Quiz 1


1
Quiz 1
  • 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 vw 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.

2
Quiz 2
  • 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 wordsexact code is not
    necessary in all cases, but try to cover all the
    steps.
  • For example
  • 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 i0 i lt b.length i)
  • System.out.println(bi)
  • -extends JApplet changes to extends JFrame

3
Quiz 3-1
  • 1. What error will Java report from the
    following code? (That iswhats wrong with this
    sequence? You do not need the actual error
    message.)
  • int a
  • a3 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
  • w2 100
  • What is the value of v2?
  • 4. Write Java code to swap integer array
    elements ai and aj.
  • 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
  • v2 has the value of 100. (w is an alias of v so
    the statement w2 100 also changes v.
  • Something like this
  • int I,J,temp
  • temp aI
  • aI aJ
  • aI temp

4
Quiz 4
  • 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

5
Quiz 5
  • 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. 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

6
  • 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 i0
  • int y2/i
  • System.out.println("Exceptions are fun")
  • catch(RuntimeException ex)
  • System.out.println("I take Exception to
    that")
  • finally
Write a Comment
User Comments (0)
About PowerShow.com