COP 2210 Sample Exam Questions Kraynek

5 pts

1. What is the difference between a class and a class object?




5 pts

2. What is the difference between an class object and an class variable?




5 pts

  1. Suppose that s1 and s2 are String variables. Explain the difference between the comparison s1.equals(s2) and the comparison s1 == s2.





5 pts

4. What is the difference between a primitive type variable and a class variable?




10 pts

5. Consider the following statements:


String s1 = new String("abcdef");

String s2 = new String("123abc");


What are the values of the following expressions?


a. s1.substring(3)

b. s1.substring(1,4)

c. s1.charAt(0) == s2.charAt(3)

d. s1.substring(0,3).equals(s2.substring(4))

e. s1 + s2


10 pts

  1. Suppose we have the declarations:

int x = 37;

String s = "abcdefb";

What are the values of the following expressions?

a. 29 % 5


b. 29 / 5


c. 5 * (x / 5) +( x % 5)

s.indexOf("b")


e. s == new String("abcdefb")



5 pts

7. What is the output of the following segment?


int n = 35;

while( true ) {

if( n <= 1 ) break;

n = n/2;

System.out.println(n);

} // end while



5 pts

8. Suppose we have the following statement:


String s = "37 FIU 67.95";


Write a code segment to extract 37 into an int variable, FIU into a String variable and 67.95 into a double variable. Hint: Use the Scanner class.









5 pts

9. Suppose we have the following statement:


double cost = someDoubleValue;


Write a code segment to output someDoubleValue using the JOptionPane method showMessageDialog so that only one digit is to the right of the decimal point.





5 pts

10. Write a Java statement that constructs a New Times-Roman bold Font with size 30 in the variable myFont.



15 pts

11. Write a main class to do the following:


Write a while loop to input Strings using JOptionPane





5 pts

12. Write a Java statement that constructs a JTextArea with 20 rows and 30 columns in the variable myOutArea.




7 pts

  1. Suppose a person's full name is in a String object referenced by the String variable fullName in the format first name, a space, a middle initial, a period, a space, last name. For example: William T. Kraynek. Write Java statement(s) to remove the middle initial, the period and one of the spaces and reference the result by the String variable firstLastName. For example if fullName refered to "William T. Kraynek" then after the Java statement(s) firstLastName would refer to "William Kraynek".








8 pts

14. Write a while loop to input numbers (ints) using the JOptionPane method showInputDialog until the user clicks cancel. Sum the numbers in a int variable sum. After the user clicks cancel display all the sum in n a JOptionPane window using the showMessageDialog method.










5 pts

15. Suppose a double value is in a double variable cash. Write a Java statement to display cash in a JOptionPane window using the method showMessageDialog in the standard currency format.


5 pts

16. What is a static member of a class?




10 pts

17. When designing a data class what kinds of methods should be considered for inclusion in the class?









10 pts

18. Suppose numbers is an ArrayList<Integer> object and has several Integers added to it. Write a code segment using a for-each loop to display each number using System.out.println.









5 pts

19. What is the output of the following segment?


int i = 0;

int j = 12;

while( i < j ) {

i += 1;

j -= 1;

System.out.println(i + " " + j);

} // end while


15 pts

20. Write a main class to do the following:

Use the Scanner class to read words (strings) from the file named Data.txt and add each one to an ArrayList<String>. After all the words have been read accumulate each one from the ArrayList<String> in a String variable out with each on a separate line and display the String out in a JOptionPane.























2. Why can't a static method call a non-static method?











3. What is displayed after the following Java code segment has been executed?

ArrayList<String> words = new ArrayList<String>();

words.add("FIU");

words.add("Miami");

words.add("Pittsburgh");

words.add("Steelers");

words.add("FSU");

words.add("Marlins");

words.add("Florida");

words.add(2,"Heat");

words.set(5,words.get(7));

words.remove(words.indexOf("FIU"));

words.remove(6);

String out = “”;

for(String s : words) out += s + “\n”;

JOptionPane.showMessageDialog(null,out);

 

4. Given ArrayList<Integer> numbers write a code segment to calculate the sum of all of the integers in numbers:











6. Given ArrayList<Integer> numbers write a code segment that calculates the smallest of all of the integers in the ArrayList<Integer> numbers.









7. Given ArrayList<Integer> numbers write a code segment that calculates the index of the smallest of all of the integers in the ArrayList<Integer> numbers. Assume that all of the integers in numbers are different.











8. Write a code segment to calculate the sum of the first 1000 numbers, that is the sum of all the integers from 1 to 1000.