Solution

advertisement
Name:
ID:
Duration:
Questions 1-17:3 points 18:49 points
CENG 102 MIDTERM III
1.
Which of the following should usually be private?
a.Methods.
b.Constructors.
c.Variables (or fields).
d.All of the above.
ANS: c. Variables (or fields).
2.
Which of the following statements is true?
a.Methods and instance variables can both be either public or private.
b.Information hiding is achieved by restricting access to class members via keyword public.
c.The private members of a class are directly accessible to the client of a class.
d.None of the above is true.
ANS: a. Methods and instance variables can both be either public or private.
3.
When should a program explicitly use the this reference?
a.Accessing a private variable.
b.Accessing a public variable.
c.Accessing a local variable.
d.Accessing a field that is shadowed by a local variable.
ANS: c. Accessing a field that is shadowed by a local variable.
4.
Having a this reference allows:
a.A method to refer explicitly to the instance variables and other methods of the object on which the method was called.
b.A method to refer implicitly to the instance variables and other methods of the object on which the method was called.
c.An object to reference itself.
d.All of the above.
ANS: d. All of the above.
5.
A constructor cannot:
a.Be overloaded.
b.Initialize variables to their defaults.
c.Specify return types or return values.
d.Have the same name as the class.
ANS: c. Specify return types or return values.
6.
Constructors:
a.Initialize instance variables.
b.When overloaded, can have identical argument lists.
c.When overloaded, are selected by number and types of parameters.
d.a and c.
ANS: d. a and c.
7.
When implementing a method, use the class’s set and get methods to access the class’s ________ data.
a.public.
b.private.
c.protected.
d.All of the above.
ANS: b. private.
8. Which statement is false?
a.The compiler always creates a default constructor for a class.
b.If a class has constructors, but none of the public constructors are no-argument constructors, and a program attempts to
call a no-argument constructor to initialize an object of he class, a compilation error occurs.
c.A constructor can be called with no arguments only if the class does not have any constructors or if the class has a public
no-argument constructor.
d.Java allows other methods of the class besides its constructors to have the same name as the class and to specify return types.
ANS: a. The compiler always creates a default constructor for a class.
9.
Not using set and get methods is:
a.A syntax error.
b.A logic error.
c.Not good program design.
d.None of the above.
ANS: c. Not good program design.
10. Using public set methods provides data integrity if:
a.The instance variables are public.
b.The instance variables are private.
c.The methods perform validity checking.
d.Both b and c.
ANS: d. Both b and c.
11. Composition is sometimes referred to as a(n) ________.
a.is-a relationship.
b.has-a relationship.
c.have-a relationship.
d.one-to-many relationship.
ANS: b. has-a relationship.
12. Static class variables:
a.Are final.
b.Are public.
c.Are private.
d.Are shared by all objects of a class.
ANS: d. Are shared by all objects of a class.
13. Which of the following is not true?
a.A static method must be used to access private static instance variables.
b.A static method has no this reference.
c.A static method can be accessed even when no objects of its class have been instantiated.
d.A static method can call instance methods directly.
ANS: d. A static method can call instance methods directly.
14. Instance variables declared final do not or cannot:
a.Use the principle of least privilege.
b.Be initialized.
c.Be modified.
d.Cause syntax errors if used as a left-hand value.
ANS: c. Be modified.
15. The term information hiding refers to:
a.public methods.
b.Hiding implementation details from clients of a class.
c.Accessing static class members.
d.The process of releasing an object for garbage collection.
ANS: b. Hiding implementation details from clients of a class.
16. A package is:
a.A directory structure used to organize classes and interfaces.
b.A mechanism for software reuse.
c.A group of related classes and interfaces.
d.All of the above.
ANS: d. All of the above.
17. The import declaration import *; ________.
a.causes a compilation error.
b.imports all classes in the library.
c.imports the default classes in the library.
d.imports the classes in package java.lang.
ANS: a. causes a compilation error.
18. Write a program in which you have a class called Balon. This class is for drawing some balons on a given environment. It will
have x,y,w,h and color variables and a draw method. Another class of type JFrame will have a Balon inside a JPanel, and a
button. When you click the button the baloon will expand 10 pixel and change its color to another random color.
import java.awt.*;
public class Balon {
int x,y,w,h;
Color c;
public Balon(int x1,int y1,int w1,int h1, Color c1) {
x=x1; y=y1; w=w1; h=h1; c=c1;
}
public void draw(Graphics g){
g.setColor(c);
g.drawRect(x,y,w,h);
g.drawOval(x,y,w,h);
g.fillOval(x,y,w,h);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class mm extends JFrame implements ActionListener{
Balon b;
JButton jb;
JPanel jp;
Random rn;
public mm(){
jp=new JPanel();
jb=new JButton("ok");
rn=new Random();
Container c=this.getContentPane();
c.setLayout(new GridLayout(1,2));
jb.addActionListener(this);
c.add(jb);
b=new Balon(100,100,100,100,new Color(255,0,0));
c.add(jp);
jp.setBackground(Color.WHITE);
setSize(500,500);
setVisible(true);
// new Timer(1000,this).start();
}
public void paint(Graphics g){
super.paint(g);
int re=rn.nextInt(256);
int gr=rn.nextInt(256);
int bl=rn.nextInt(256);
b.c=new Color(re,gr,bl);
b.draw(jp.getGraphics());
}
public void actionPerformed(ActionEvent e){
b.w=b.w+20;
b.h=b.h+20;
repaint();
}
public static void main(String[] args) {
mm f=new mm();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Download