PIC20A 2001 Midterm Class 2

Each question worths 2pts
Use a pencil to circle the letter for the correct answer
1) What is the output of the following code?
1. int a = 1, b = 2, c = 3;
2. if( b > c || c >= a) {
3.    System.out.println(c);
4. } else {
5.    System.out.println(b);
6. }
A. 1
B. 2
C. 3
D. compiling error at line 2.


2) What is the output of the following lines
1. String name = "Charles";
2. int age = 18;
3. System.out.println("name :" + name);
4. System.out.println("age :" + age);
A.
name :Charles
age :18
B.
name :Charles age :18
C. compiling error.
D. none of the above.


3) What is the correct way of using Font?
A. Font f = new Font(Font.serif, Font.BOLD, 10);
B. Font f = new Font("Serif", Font.BOLD + Font.ITALIC, 10);
C. Font f = new Font(Font.serif, Font.bold, 25);
D. Font f = new Font("Serif", Font.bold, 30);


4) Suppose string1 and string2 are String. If the output of the following 2 lines
System.out.println(string1);
System.out.println(string2);
are the same.
What is the output of the following lines
if(string1.equals(string2)) {
   System.out.println("a");
} else {
   System.out.println("b");
}
A. a
B. b
C. compiling error
D. cannot be predicted.


5) What is the output of the following lines
1. float f = 1.234;
2. System.out.println("The number is " + f + " .");
A. compiling error.
B. The number is 1.234 .
C. The number is 1.234000 .
D. The result cannot be predicted because we don't know the output format of the decimal number.


6) What is the output after we run java MainClass
public class MainClass {
   public static int main() {
      System.out.println("main 0");
   }
   
   public void main(String s) {
      System.out.println("main 1");
   }
   
   public static void main(String[] args) {
      System.out.println("main 2");
   }
}
A. main 0
B. main 1
C. main 2
D. compiling error


7) I have 2 classes in 2 different files. What is the output after we run java Test?
public class Test {
   public static void main(String[] args) {
       double d = 12.3;
       Decrementer dec = new Decrementer();
       dec.decrement(d);
       System.out.println(d);
   }
}

public class Decrementer {
   public void decrement(double decMe) {
       decMe = decMe - 1.0;
    }
}
A. 0.0
B. -1.0
C. 12.3
D. 11.3


8) Consider the following line of code
int[] x = new int[25]
After execution, which statement is ture?
A. x[24] is 0
B. x[24] is undefined
C. x[25] is 0
D. x[0] is null


9) What is the output of the following program after we run java StaticTest
public class StaticTest {
    private static int x = 100;
    
    public static void main(String[] args) {
        StaticTest t1 = new StaticTest();
	t1.x++;
	StaticTest t2 = new StaticTest();
	t2.x++;
	t1 = new StaticTest();
	t1.x++;
	StaticTest.x++;
	System.out.println("x = " + x);
    }
}
A. Line 8 cannot be compiled because it is a static reference to a private variable.
B. the program compiles, and the output is x = 102
C. the program compiles, and the output is x = 103
D. the program compiles, and the output is x = 104


10) Which statement is true about the following code fragment?
int j = 2;
switch (j) {
   case 2:
     System.out.println("value is two");
   case 3:
     System.out.println("value is three");
     break;
   default:
     System.out.println("value is " + j);
     break;
}
The output of the program is
A. value is two
B. value is two followed by the text value is three
C. value is two followed by the text value is three, followed by the text value is 2
D. value is 2


11)
public class Super {
   protected int a ;
   protected int b;
   public Super(int a, int b) {
      this.a = a;
      this.b = b;
   }
   
   public Super(int a) {
     this(a,3);
   }
}

public class Sub extends Super{
   private int c;
   public Sub(int c) {
     Super(5);
     this.c = c;
   }
   
   public static void main(String[] args) {
       System.out.println(new Sub(10).b);
   }
}
What is the output of java Sub ?
A. 3
B. 5
C. 10
D. 0


12)
import java.awt.*;
import javax.swing.*;

public class LayoutTest extends JFrame {
    JButton button1 = new JButton("Mac");
    JButton button2 = new JButton("Window");
    JButton button3 = new JButton("Unix");
    JButton button4 = new JButton("OS2");
    JButton button5 = new JButton("Linux");
    
    public LayoutTest() {
	getContentPane().setLayout(new GridLayout(2,3));
	getContentPane().add(button1);
	getContentPane().add(button2);
	getContentPane().add(button3);
	getContentPane().add(button4);
	getContentPane().add(button5);
	getContentPane().add(new JButton("Java"), 4);
	getContentPane().remove(2);
   }
   
   public static void main(String[] args) {
       JFrame f= new LayoutTest();
	f.pack();
	f.setVisible(true);
   }
}
What is the output after we run java LayoutTest?

A.
a
B.
b
C.
c
D.
d


13) dog.gif stores the picture of the dog.
import java.awt.*;
import javax.swing.*;

public class LabelTest extends JFrame {
   public static void main(String[] args) {
       JFrame f= new ButtonTest();
       JLabel label = new JLabel("Hi", new ImageIcon("dog.gif"),JLabel.CENTER);
       label.setHorizontalTextPosition(JLabel.CENTER);
       label.setVerticalTextPosition(JLabel.BOTTOM);
       f.getContentPane().add(label);
       f.setSize(100,100);
       f.setVisible(true);
   }
}
What is the output after we run java LabelTest?

A.
a

B.
b
C.
c
D.
d


14) Which of the following is not a constructor for FlowLayout?
A. FlowLayout()
B. FlowLayout(int alignment)
C. FlowLayout(int hgap, int vgap)
D. FlowLayout(int alignment, int hgap, int vgap)


15) Which of the following are correct names of identifies ?
(i) student_names
(ii) class-name
(iii) 2names
(iv) abstract
A. (i),(ii) only
B. (i),(ii),(iv) only
C. (i) only
D. (i) and (ii) only






Programming Question 1(30 pts)
Write a class ShowShape extends JFrame. Such that
java ShowShape circle displays a circle with upper left corner at (50,50) and diameter 100
with red color.
java ShowShape square displays a square with upper left corner at (50,50) and length 100
with yellow color.
And java ShowShape other strings displays nothing.
Here is part of ths code :
 
// import statements





public class ShowShape extends JFrame {
     private String shape;
     // your constructor
     
     

     
 


    // paint method
     
     

     
     
     
     
     



     // main method
     public static void main(String[] args) {
        ShowShape s;
        // your code



	
	
	
	
	s.setSize(200,200);
	s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	s.setVisible(true);
   }
}
Programming Question 2(40 pts)
Write a class NumberButton extends JPanel. It has the following constructor
public NumberButton(int row, int col, int startNum)
It creates row*col buttons. The buttons are added from left to right, top to button. The text on the buttons are numbers, starts with startNum and the number is increased by 1 on the next button.
It has another constructor.
public NumButton(int row, int col) 
The startNum is set to 0.
Here are some examples:
numbutton1
NumberButton(3,3,19)
numbutton2
NumberButton(2,4,101)
numbutton3
NumberButton(3,2)

You can write your answer on next page if you don't have enough space here.