CSC210 Final Exam Sp15 Sample

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

CSC210-1200 Sample Final Exam

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");

2) Suppose the class Sub extends the class Sandwich.


Sub sub=new Sandwich();

3)
Scanner fileIn=new Scanner(new File(input.txt));
int input=fileIn.nextInt();
catch(InputMismatchException e)
{
System.out.println(e.getMessage());
}
finally
{
fileIn.close();
}

CSC210-1200 Sample Final Exam

4)
try
{
}

File inFile=new File(filename);

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

}
}

CSC210-1200 Sample Final Exam


2. (30 points) Short answers.
1) What will be the value of loc after the following code is executed?
a. int loc;
b. String str = "The cow jumped over the moon.";
c. loc = str.indexOf("o");

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

public class ClassA


{
public ClassA() {}
public void method1(int a){}
}
public class ClassB extends ClassA
{
public ClassB(){}
public void method1(){}
}
public class ClassC extends ClassB
{
public ClassC(){}
public void method1(){}
}

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;

CSC210-1200 Sample Final Exam


try
{
number = Integer.parseInt(input);
}
catch(NumberFormatException ex)
{
number = 0;
}
catch(RuntimeException ex)
{
number = 1;
}
catch(Exception ex)
{
number = -1;
}
System.out.println(number);

6) If panel is a JPanel object, write a statement to make its background blue?

2. (20 points) Write java methods for the following:


1) Write a method verifyPassword (String password) that validates if a password meets the
following criteria:
a. The password should be six to eight characters long.
b. The password should contain at least one uppercase letter and at least one
lowercase letter.
c. The password should have at least one digit.

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.

CSC210-1200 Sample Final Exam

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.

You might also like