Analyze the following code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test1 extends JFrame {
public Test1() {
add(new MyCanvas());
}
public static void main(String[] args) {
JFrame frame = new Test1();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyCanvas extends JPanel {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(message, 20, 20);
}
}
A. The program runs fine and displays nothing since you have not set a string value.
B. The program would display Welcome to Java! if you replace new MyCanvas() by new MyCanvas("Welcome to Java!").
C. The program has a compile error because new Test1() is assigned to frame.
D. The program has a NullPointerException since message is null when g.drawString(message, 20, 20) is executed.
The correct answer is D
Explanation: (B) is incorrect since MyCanvas does not have a constructor with a string argument. (C) is incorrect since new Test1() is an instance of JFrame so it is fine to assign it to frame.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test1 extends JFrame {
public Test1() {
add(new MyCanvas());
}
public static void main(String[] args) {
JFrame frame = new Test1();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyCanvas extends JPanel {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(message, 20, 20);
}
}
A. The program runs fine and displays nothing since you have not set a string value.
B. The program would display Welcome to Java! if you replace new MyCanvas() by new MyCanvas("Welcome to Java!").
C. The program has a compile error because new Test1() is assigned to frame.
D. The program has a NullPointerException since message is null when g.drawString(message, 20, 20) is executed.
The correct answer is D
Explanation: (B) is incorrect since MyCanvas does not have a constructor with a string argument. (C) is incorrect since new Test1() is an instance of JFrame so it is fine to assign it to frame.
Java
- In Java, the mechanism that allows you to combine data and operations on the data into a single unit is called a(n)
- Which of the following is a legal way to declare and instantiate an array of 10 Strings?
- The statement System.out.println(values[7]); will
- Which of the following loops would adequately add 1 to each element stored in values?
- What is the value of values.length?
- What is returned by values[3]?
- A listener is an object that
- JOptionPane is a class that provides GUI
- In order to have some code throw an exception, you would use which of the following reserved words?
- An exception can produce a “call stack trace” which lists
- A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception?
- Assume that you are defining a class and you want to implement an ActionListener. You state addActionListener(this); in your class’ constructor. What does this mean?
- In order to implement Comparable in a class, what method(s) must be defined in that class?
- Which of the following interfaces would be used to implement a class that represents a group (or collection) of objects?
- An object that refers to part of itself within its own methods can use which of the following reserved words to denote this relationship?
- Static methods cannot
- Which of the following methods is a static method? The class in which the method is defined is given in parentheses following the method name.
- An object may be made up of other objects.
- Method decomposition is the process of creating overloaded versions of a method that do the same thing, but operate on different data types.
- The println method on System.out is overloaded.
- A method defined without a return statement will cause a compile error.
- If a method takes a double as a parameter, you could pass it an int as the actual parameter.
- The different versions of an overloaded method are differentiated by their signatures.
- The number and types of the actual parameters must match the number and types of the formal parameters.
- The return statement must be followed a single variable that contains the value to be returned.