Each question worths 1pts
Use a pencil to circle the correct answer
1) What is the output of the following code?
1. int a = 1, b = 2, c = 3;
2. if(c > b && 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) 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) {
       Holder h = new Holder();
       h.held = 100;
       h.bump(h);
       System.out.println(h.held);
   }
}

public class Holder {
   public int held = 0;
   public void bump(Holder theHolder) {
     theHolder.held++;
   }
}
A. 0
B. 1
C. 100
D. 101


3) What is the output of the following code?
String[] names = {"Amy", "Ben", "Charles"};
name[1] += ".";
for(int i = 0; i < names.length; i++) {
   System.out.println(names[i]);
}
A. The output includes Amy. with a trailing period
B. The output includes Ben. with a trailing period
C. The output includes Charles. with a trailing period
D. none of the outputs show a trailing period


4) 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.       OverloadTest 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


5) What is the output of the following code?
int x = 0, y = 4, z = 5;
if(x > 2) {
   if(y < 5) {
     System.out.println("message one");
   }  else {
     System.out.println("message two");
   }
} else if ( z > 5) {
      System.out.println("message three");
} else {
      System.out.println("message four");
}

A. message one
B. message two
C. message three
D. message four


6) Consider the following class
public class A {
    public int x = 0;
    public int y = 1;
    public char ch = 'A';
    public A(int x, int y) {
        this.x = x;
	this.y = y;
    }
    
    public A(int x, char ch) {
        this(x,0);
	this.ch = ch;
   }
   
   public A(int x) {
       this(x, 'A');
   }
   
   public static void main(String args) {
       A a = new A(5);
       System.out.println(a.y);
   }
}

What is the output of java A?
A. 0
B. 5
C. 1
D. unknown


7) What is the output of the following program after you run java Test1 ?
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

8) 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


9)
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


10) Which of the following are correct names of identifies ?
(i) student_names
(ii) class-name
(iii) 2names
(iv) final
A. (i),(ii) only
B. (i),(ii),(iv) only
C. (i) only
D. (i) and (ii) only
Programming Question 1(10 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 (15pts)
Create a class called Match.
Match string0 string1 string2... stringN displays strings from string1 to... stringN that contains at least one 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.
public char[] toCharArray():
Converts this string to a new character array.
public char charAt(int index)
Returns the character at the specified index.