0% found this document useful (0 votes)
20 views

Midterm Spring 2020

This document contains instructions and questions for a Java programming midterm exam. It includes 10 true/false questions worth 2 points each, 10 multiple choice questions worth 2 points each, and 4 descriptive questions worth 10 points each. The questions cover topics like applets, Swing components, layout managers, event handling, and differences between applications and applets.

Uploaded by

蒋宇博
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Midterm Spring 2020

This document contains instructions and questions for a Java programming midterm exam. It includes 10 true/false questions worth 2 points each, 10 multiple choice questions worth 2 points each, and 4 descriptive questions worth 10 points each. The questions cover topics like applets, Swing components, layout managers, event handling, and differences between applications and applets.

Uploaded by

蒋宇博
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

CS602 – Java Programming

Mid Term, spring 2020

Name: Anmol Nahariya

Instructions:

1. Download the test on your computer, save, do your work, save again, and then upload through canvas.

2. Must write your name

3. Must submit through Canvas, on time. No email and late submission will be accepted

4. If any of your descriptive answer is similar to any other student both tests will be void

5. If any answer is a copy paste, the test will be void (write in your own words where necessary).

True / False questions (2 points each):

1. Applet runs inside browsers and can’t run as a client side. FALSE

2. Containers are special GUI components that hold and manage other components. TRUE

3. Radio buttons doesn’t need to operate as a group to provide a set of mutually exclusive options. FALSE

4. To implement the listener interface and override its methods are required to perform event handling.
TRUE

5. A GUI’s appearance is a function of the containment hierarchy and the layout managers of each container.
TRUE

6. SWING has more powerful components like tables, lists, scroll panes, color chooser, etc. FALSE

7. Applet is a java program that can be transported across a network and executed only by using a web
browser. TRUE

8. A layout manager can be explicitly set for each container. TRUE

9. The SetLayout() method is basically used to specify a container’s layout. TRUE

10. A listener may have to provide empty method definitions for unheeded events to satisfy the interface.
TRUE

Multiple Choice questions – there is only one correct answer for each question (2 points each):

1. Which of the following component subclasses support painting:

A. The Canvas

B. Frame

C. Panel

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
D. All of the above - CORRECT ANSWER

2. Which of the following components may have a number of choices on the window?

A. Checkbox - CORRECT ANSWER

B. Labels

C. List

D. Textarea

3. How can a GUI component handle its own events:

A. By implementing the corresponding event-listener interface - CORRECT ANSWER

B. By writing your own appropriate java code

C. By just adding event listener

D. None of the above

4. Which is the container that contains title bar and can have MenuBars as well as other components like
button, textField etc.

A. Window

B. Frame - CORRECT ANSWER

C. Panel

D. Container

5. The following specifies the advantages of:

It is light weight

It supports pluggable look and feel

It enables GUI program to add various components

A. AWT

B. SWING - CORRECT ANSWER

C. Both A & B

D. None of the above

6. Which classes can an Applet extend?

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
A. java.applet.Applet class

B. java.swing.JApplet class

C. java.awt.Panel class

A. All of the above - CORRECT ANSWER

7. You have a Swing label that tends to have more horizontal space than it needs to display its text. What
code would you use to make the text within a label (Jlabel) be centered?
A. new JLabel(labelText, JLabel.CENTER);
A.label.setHorizontalAlignment(JLabel.CENTER);
A. All of the above - CORRECT ANSWER
B. None of the above

8. What Is The Difference Between Applications and Applets?

A. Application runs explicitly within JVM whereas Applet loads and runs itself automatically in a java-en-
abled browser.

B. Application starts execution with its main method whereas applet starts execution with its init
method.

C. Application can run with or without graphical user interface whereas applet must run within a graphi-
cal user interface.

D. All of the above. - CORRECT ANSWER

9. Following are some of the subclasses of javax.swing.JComponent except:


A. JTextComponent
B. JSlider
C. JComponent - CORRECT ANSWER
D. JToolTip

10. Every Swing component relies on:


A. AWT Container - CORRECT ANSWER
B. AWT Component
C. AWT Tool
D. AWT Code

Descriptive questions and code snippets (10 points each):

1. What are some of the restrictions of an Applet (list at least five)?

ANSWER:

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
1. Cannot load libraries or define native methods.

2. Cannot make network connections except to the host that it came from.

3. Cannot read certain system properties.

4. Cannot ordinarily read or write files on the execution host.

5. Cannot start any program on the host that’s executing it.

2. Write the code that implements the invokeAndWait() and invokeLater() method of Swing API (one line
code for each method). Explain in your own words the main difference between the two.

ANSWER:

SwingUtilities.invokeAndWait(runnable_name);

SwingUtilities.invokeLater(runnable_name);

InvokeAndWait will block until the task is completed while InvokeLater is a


non-blocking call.
InvokeLater is used to perform a task asynchronously in AWT Event dispatcher
thread while InvokeAndWait is used to perform task synchronously.

3. Complete the following code to display an applet, so that the contents are contained in a rectangle around
the phrase "Exercise Applet". Use starting coordinates 5, 15 for the string.

public void paint(Graphics g) {

//Draw a Rectangle around the applet's display area.

g.drawRect(2,2,30,40);

//Draw the current string inside the rectangle.

g.drawString(“Exercise Applet”, 5, 15);

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
4. Complete the initUI() method with your codes to show that when we click on a button with label “Quit”,
the application terminates (this is not a complete program and that’s fine).

public class QuitButtonEx extends JFrame {

public QuitButtonEx() {

initUI();

private void initUI() {

var quitButton = new JButton("Quit");

//add code to add action listener

quitButton.addActionListener((event) -> System.exit(0));

//add code to create layout

createLayout(quitButton);

//add code to set title "Quit button"

setTitle("Quit button");

//add code to set size

setSize(300, 200);

//add code to set relative location

setLocationRelativeTo(null);

//write code to exit on close

setDefaultCloseOperation(EXIT_ON_CLOSE);

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
What is the output of the following programs (5 points each):

1. This is
a two part question. First explain what the following program is intended to do? Then write what will be
the output of this program?
Note that the code is incomplete and you don’t have to correct it or find errors.

public class LabelExample extends Frame implements ActionListener{


JTextField tf; JLabel l; JButton b;
LabelExample(){
tf=new JTextField();
tf.setBounds(50,50, 150,20);
l=new JLabel();
l.setBounds(50,100, 250,20);
b=new JButton("Find IP");
b.setBounds(50,150,95,30);
b.addActionListener(this);
//add all components and set size, layout and visibility
}
public void actionPerformed(ActionEvent e) {
try{
String host=tf.getText();
String ip=java.net.InetAddress.getByName(host)
.getHostAddress();
l.setText("IP of "+host+" is: "+ip);
}catch(Exception ex){System.out.println(ex);}
}
public static void main(String[] args) {
new LabelExample();
} }

Answer: This program is intended to display the ip address of the host.


This program creates a textfield, label and a button. The programs takes the host name
from the textfield and after clicking the button, the label text is set as “IP of ‘host_name’ is ‘ip_ad-
dress’”.

1. What does the following code results when it’s executed?

Note that you do not have to complete the code or find any error in it.

public class Example extends JFrame {

//constructor

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
public final void initUI() {

JPanel panel = new JPanel();


getContentPane().add(panel);

panel.setLayout(null);
panel.setToolTipText("A Panel container");

JButton button = new JButton("Button");


button.setBounds(100, 60, 100, 30);
button.setToolTipText("A button component");

panel.add(button);
setTitle("Tooltip");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String[] args) {


public void run() {
Example ex = new Example();
ex.setVisible(true);
}
}
}

Answer: The code creates a JPanel containing a button named “Button”. When we move
the mouse pointer to the button the tool tip pops up a message as “A button
component” and when we move the mouse pointer to the panel container the tool
tip pops up a message as “A Panel Container”.

1. What is the output of the following program?

Note that this is a partial program and you do not need to complete it.

public class LeftRight {

//Variables

public static void main (String[] args) {

JFrame frame = new JFrame (“Left Right”);


frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new LeftRightPanel());
frame.pack();
frame.setVisible(true);
}
}

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
Answer: This program creates a JFrame named as “Left Right”. We exit the frame when we click on the
close button as the setDefaultCloseOperation is set as EXIT_ON_CLOSE.

2. What will be the output of the following program.

Note that it’s an incomplete program and you don’t need to correct it.

public class CheckBoxExample


{
CheckBoxExample(){
JFrame f= new JFrame("CheckBox Example");
JCheckBox checkBox1 = new JCheckBox("C++");
checkBox1.setBounds(100,100, 50,50);
JCheckBox checkBox2 = new JCheckBox("Java", true);
checkBox2.setBounds(100,150, 50,50);

//add components, set size, layout and visibility

}
public static void main(String args[])
{
new CheckBoxExample();
}
}

Answer: This program creates a JFrame named CheckBoxExample. It has two checkboxes named “C++”
and “Java”. The checkbox named java is set as checked as its value is true.

This study source was downloaded by 100000853042928 from CourseHero.com on 03-12-2023 10:20:04 GMT -05:00

https://www.coursehero.com/file/74463433/midtermSpring2020docx/
Powered by TCPDF (www.tcpdf.org)

You might also like