PIC20A Midterm for students that can't take test 1 and test 2

1) What is the output of the following lines?
1. int a = 1, b = 2, c = 3;
2. if(c > b && c >= a && b < 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. String result = "Charles" + " is " + age;
4. System.out.println(result);
A.
Charles is 18
B.
Charles is
C. compiling error at line 3 because integer cannot be added to String.
D. compiling error at line 4 because result is of type int.


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) What is the output of the following code if we run java Temp?
public class Temp {
    public static void main(String[] args) {
	String a = "Charles";
	String b = "Charle";
	String c = b + 's';
	if(a == c) {
            System.out.println("same");
        } else {
            System.out.println("different");
       }
   }
}
A. same
B. different
C. cannot be predicted.
D. compiling error at the line String c = b + 's' because we can't add char to String

5. Consider the following program
int a = 1.3;
double b = 2.01;
XXX c = a/b;
XXX can be
a) int only
b) double only
c) int or double
d) compiling error becasue we can't do division between int and double.


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) {
       Tes2 t = new Test2();
       t.num = 100;
       t.triple(t)
       System.out.println(t);
   }
}

public class Test2 {
   public int num = 1;
   public void triple(Test2 test) {
     test.num*=3;
   }
}
A. 1
B. 3
C. 100
D. 300


8) Consider the following line of code
double[] x = new double[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 program after you run java OverloadTest?
 1.public class OverloadTest {
 2.  void overload(int i) {
 3.     System.out.println("int version");
 4.  }
 5.  
 6.  void overload(String s) {
 7.     System.out.println("string version");
 8.  }
 9.  
10.   public static void main(String[] args) {
11.       OverrideTest test = new OverloadTest();
12.       int a = 100;
13.       test.overload(a);
14.  }
15.}   
A) The code can be compiled but an error occurs when you try to run it.
B) The code will be compiled and the output is int version
C) The code will be compiled and the output is string version
D) the code cannot be compile because methods can't share the same name


10) What is the output of the following code?
int i = 1;
while(i < 20) {
   System.out.print(i);
   i*=2;
}
A. 124816
B. 12345678910111213141516171819
C. Infinite loop
D. 24816


11) Consider the following program,
public class Super {
   protected int a ;
   protected int b;
   public Super(int a, int b) {
      this.a = a;
      this.b = b;
   }
   
   public Super() {
     this(1,2);
   }
}

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


12) What is the output of the following program
import java.awt.*;
import javax.swing.*;

public class Test1 extends JFrame {
    JButton button1 = new JButton("Mac");
    JButton button2 = new JButton("Window");
    JButton button3 = new JButton("Unix");
    JButton button4 = new JButton("OS2");
    
    public Test1() {
	getContentPane().setLayout(new FlowLayout());
	getContentPane().add(button1);
	getContentPane().add(button2);
	getContentPane().add(button3);
	getContentPane().add(button4);
	getContentPane().add(new JButton("Java"), 3);
	getContentPane().add(new JButton("C++"), 2);
   }
   
   public static void main(String[] args) {
       JFrame f= new Test1();
	f.pack();
	f.setVisible(true);
   }
}
A.
a

B.
b

C.
c

D.
d
13) dog.gif stores the picture of the dog.
What is the output after we run Java LabelTest ?
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);
   }
}

A.
a

B.
b
C.
c
D.
d


14)
Which one of the following is not a method form Graphics g ?
A. drawString
B. setColor
C. setBackground
D. fillArc

15)
Given a string by calling s = new String("xyz"), which of the following modify the string?
A. s.replace('z','a') .
B. s.substring(3).
C. Both A and B.
D. none of the above.



Programming Question 1(30 pts)
Create a class ButtonMidterm, it extends JPanel. and has the following class variables BORDER and FLOW of type integer.
It has only one constructor: public ButtonMidterm(int layout) The layout can be BORDER or FLOW.
If the user set the layout be BORDER, then the JPanel has 5 JButtons (with text "button") occupied north, south, east, west, and center.
If the layout is FLOW, then the JPanel has 5 JButtons (with text "button").
Here is a portion of the code
public class ButtonMidterm extends JFrame {
    private int layout;
    // your codes
    
    
 
   
    
    
    
    public class ButtonMidterm(int layout) {
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    }
 
}


Programming Question 2 (40pts)
Create a class called Match.
Match string0 string1 string2... stringN displays the strings from string1 to... stringN such that stringi contains at least on character from string0. The program is case sensitive.
Here is output
C:> java Match o apple orange banana
orange
C:> java Match op apple orange banana
apple orange
C:> java Match rstu Russia China Japan France England Korea Canada
Russia France Korea
C:> java Match xyz Russia China Japan France England Korea Canada
        <--------------nothing shows out 
C:> java Match 
        <--------------nothing shows out 
Here are some useful methods :
class String public int indexOf(int ch): Returns the index within this string of the first occurrence of the specified character. If a character with value ch occurs in the character sequence represented by this String object, then the index of the first such occurrence is returned. If no such character occurs in this string, then -1 is returned.