CSC210 Final Exam Sp15 Sample
CSC210 Final Exam Sp15 Sample
CSC210 Final Exam Sp15 Sample
Name__________________________
1. (30 points) Please find and correct the error(s) in each of the following code segment.
1) The following statements will replace Chicago to Tampa.
StringBuilder str =
new StringBuilder("We have lived in Chicago, " +
"Trenton, and Atlanta.");
str.replace(17, 23, "Tampa");
3)
Scanner fileIn=new Scanner(new File(input.txt));
int input=fileIn.nextInt();
catch(InputMismatchException e)
{
System.out.println(e.getMessage());
}
finally
{
fileIn.close();
}
4)
try
{
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
catch(FileNotFoundException e)
{
...
}
5)
//display an image on the panel
JPanel panel=new JPanel(summer.jpg);
6) The following is an inner class that will be registered as an item listener for a JRadioButton
component:
private class Listener implements ItemListener
{
public void actionPerformed(ActionEvent e)
{
}
}
2) What will be the displayed after the following statements are executed.
a. String str=cookies>milk>fudge:cake:ice cream;
b. String tokens=str.split([>:]+);
c. System.out.println(tokens.length);
3) Look at the following code:
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
Line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Which method1 will be executed when the following statements are executed?
ClassA item1 = new ClassB();
item1.method1();
4) In the following code, what will the call to super do?
public class ClassB extends ClassA
{
public ClassB()
{
super(40);
System.out.println("This is the last statement "+
"in the constructor.");
}
}
5) What will the following code display?
String input = "99#7";
int number;
2) Write a method calcAverge (double[] scores), which accepts an array of scores as the
argument and returns the average scores. If any score in the array is less than 0 or greater
than 100, the method should throw an exception of the Exception class with the error
message An invalid score found.
3. (20 points) Create a frame with two buttons, called Expand and Shrink. When the Expand
button is clicked, the frame expands by 10 percent. When the Shrink button is clicked, the
frame shrinks by 10 percent.