PIC 20 A: 2001 Midterm for Class 1
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(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) What is the output of the following code?
1. String name = "Charles";
2. int age = 18;
3. System.out.println("name :" + name + "\n" + "age :" + age);
A.
name :Charles
age :18
B.
name :Charles age :18
C.
name :Charles\nage :18
D. compiling error at line 3.
3) What is the correct way to draw a circle with radius 5 and center at (10,20)?
A. fillCircle(10,20,5)
B. fillOval(10,20,5,5)
C. fillOval(5,15,5,5)
D. fillOval(5,15,10,10)
4)
Suppose string1 and string2 are String.
If the output of the following code
System.out.println(string1);
System.out.println(string2);
are the same.
What is the output of the following lines
if(string1 == string2) {
System.out.println("a");
} else {
System.out.println("b");
}
A. a
B. b
C. cannot be predicted.
D. compiling error.
5) Consider the following program
int a = 1;
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 add int and double.
6)
Which one of the following signatures is valid for the main method
to be an entry point of an application?
A. public static void main()
B. public static void main(String[] arg)
C. public void main(String[] arg)
D. public static int main(String[] arg)
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) {
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
8)
What is the output of the following code?
int x = 1;
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
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. 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
10)
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
11)
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
12) 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.
B.
C.
D.
13)
import java.awt.*;
import javax.swing.*;
public class ButtonTest extends JFrame {
public static void main(String[] args) {
JFrame f= new ButtonTest();
JButton button = new JButton("Hi");
button.setHorizontalAlignment(JButton.LEFT);
button.setVerticalAlignment(JButton.BOTTOM);
f.add(button);
f.setSize(90,90);
f.setVisible(true);
}
}
What is the output of the program after you run java ButtonTest?
A.
B.
C. Error occurs
D.
14)
Which one of the following is not a constructor for JLabel
?
A. JLabel()
B. JLabel(Icon image)
C. JLabel(String text, Icon image)
D. JLabel(String text Icon image, int alignment)
15)
Given a string by calling s = new String("xyz"),
which of the following modify the string?
A. s.replace('z','a') only.
B. s.substring(3) only.
C. both A and B.
D. none of the above.
Programming Question 1(30 pts)
Write a class Midterm such that it displays the following
output

Here is part of the code
import java.awt.*;
import javax.swing.*;
public class Midterm {
public static void main(String[] args) {
JFrame f = new JFrame();
// your code here
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(240,130);
f.setVisible(true);
} // end main
} // end class
Programming Question 2 (40pts)
Write a class ChangeCase such that
java ChangeCase string0 string1 ... stringN
changes lowercase letters of the strings to uppercase letters
and change uppercase letters of the strings to lowercase letters.
Then display the new strings.
Here are some sample outputs.
C:> java ChangeCase Charles
cHARLES
C:> java ChangeCase aBcDeF gHiJkL
AbCdEf GhIjKl
C:> java ChangeCase Pic10A US123@hotmail.com 1a2B3c
pIC10a us123@HOTMAIL.COM 1A2b3C
C:> java ChangeCase
C:> <--------- nothing happen
Here are some useful methods.
class String
String(char[] charArray) : constructor, construct a String out of the array.
char[] toCharArray() : converts this string to a new character
array.
public char charAt(int index) :
returns the character at the specified index.
The first character of the sequence is at index 0, the next at index 1,
and so on, as for array indexing.
class Character
static boolean isUpperCase(char ch) :
determines if the specified character is an uppercase character.
static boolean isLowerCase(char ch) :
determines if the specified character is a lowercase character.
static char toUpperCase(char ch) :
the given character is mapped to its uppercase equivalent and the uppercase character is returned.
If the character has no uppercase equivalent, the character itself is
returned.
staic char toLowerCase(char ch) :
the given character is mapped to its lowercase equivalent and the lowercase character is returned.
If the character has no lowercase equivalent, the character itself is
returned.
Hint
It should be helpful to write a
static String changeCase(String s) method. It returns a new string
by changing the lowercase(resp. uppercase) letters of s to uppercase(resp.
lowercase) letters.
You can write your answer on next page if you don't have enough space here.