Inclass Final Review Flashcards

1
Q

Write a recursive method that returns the number of the uppercase letters in a string using the following method header:
public static int countUpperCase(String l)

for example, this is the main:
countUpperCase(“ABc”) //This should return 2

A

public static int countUpperCaseLetter(String l){
int count = 0;
for(int i = 0; i < l.length(); i++){
char c = l.charAt(i);
if(Character.isUpperCase(c)){
count ++;
}
}
return count;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is wrong with the following code:
a.
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println(x.compareTo(new Integer(4))

b.
Number x = new Integer(3);
System.out.println(x.intValue());
System.out.println((Integer)x.compareTo(new Integer(4))

A

a. CompareTo needs to be inherited

b. (Integer) casting gets beat by the x.compareTo in precedence.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

try {
s1
s2
s3
}
catch(Exception e1){
}
catch(Exception e2){
}
s4

a. If s2 causes an exception, will s3 run?
b. if s2 causes an exception that is not caught will s4 run?

A

a.

b. No, s4 will only run if the try block completes or if an error is properly caught. The only way code is executed after an uncaught exception happens

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is output:
Interface A{
void print()
}
class C{}
class B extends C implements A{
public void print(){}
}

public class test{
public static void main(String []args){
B b = new B();
if(b instanceof A)
System.out.println(“b is an instance of A”);
if(b instanceof C)
System.out.println(“b is an instance of C”_;

a. None
b. b is an instance of A
c. b is an instance of C
d. b is an instance of A followed by b is an instance of C

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Suppose C is an interface, B is an abstract class that partially implements C, and A is a concrete class with a default constructor that extends B. Which of the following is correct?

a. Aa = new C()
b. A a = new B()
c. C b = new A()
d. B b = new B()

A

c. Because A invokes the default constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following is correct?
a. An abstract class does not contain constructors
b. The constructor in an abstract class must be protected
c. Constructor in an abstract class must be private

A

b.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

public class Test{
public Object max(Object o1, Object o2){
if((Comparable)o1.compareTo(o2) >= 0) {
return o1
}
else{
return o2
}
}
}
a. The program has a syntax error because no main method
b. The program has a syntax error because o1 is an Object instance and it does not have the compareTo method
c. The program has a syntax error because you cannot cast an Object instance o1 into Comparable
d. The program would compile if((Comparable)o1.compareTo(o2) >= 0)
is replaced by (((Comparable)o1).compareTo(o2) >= 0)
e. Both b and d are correct

A

E. Both of b and d are the most difficult

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly