CMSC132 Exam #1 Questions With 100% Correct Answers 2024
Classes can only extend more than one other class if at least one of those classes is an abstract class. - ANSFalse - a Class can never extend more than one class. Downcasting can be done implicitly. - ANSFalse - it must be done explicitly or your code will not compile. "Sideways" casting is never allowed - ANSTrue '' will refer to the current object, treating it as an instance of the parent of the parent of the current class. - ANSTrue. For example, if class C extends class B, which extends class A, then calling "String()" from within class C will return the result of A's toString method. Constructors in subclasses must directly invoke the constructors of every single parent class before initializing the instance variables unique to that subclass. - ANSFalse - usually, a single call to the constructor of the immediate superclass is needed, however. This is done with the keyword 'super' and must be the first line in the constructor. . Assume there is a method named foo in a superclass A, which has been overridden in B (a subclass of A). If () is called from within class B, it will run the foo() method from class A, but if foo() is called from within class B, it will run the foo() method from class B. - ANSTrue Every class is a subclass of Object. - ANSTrue - note that this question doesn't necessarily mean that the class is ONLY a subclass of Object, but every class does inherit from Object, either directly or indirectly. When you override a method, you are allowed to change the number of arguments. - ANSFalse - the method signature must be exactly the same When you overload a method, you are allowed to change the number of arguments. - ANSTrue To sort an ArrayList of MyObject objects using C, MyObject must have an implementation of the compareTo method defined in the class. - ANSFalse - you can also pass in a Comparator with the compare method. Unchecked exceptions are generally for common exceptions that need to be accounted for. - ANSFalse - this is describing checked exceptions, not unchecked exceptions. An abstract class must include at least one abstract method. - ANSFalse An abstract class can contain instance variables and constructors. - ANSTrue You can implement more than one abstract class. - ANSFalse - You can EXTEND only one abstract class. An abstract class can be instantiated. - ANSFalse An interface can be instantiated. - ANSFalse A Java class can implement multiple interfaces. - ANSTrue Many Java classes can extend the same superclass. - ANSTrue - however, a single Java class cannot extend multiple superclasses. Make sure you understand this distinction. Many classes can implement the same interface. - ANSTrue Public constructors can be declared in interfaces. - ANSFalse Explicit upcasts can sometimes cause a ClassCastException to be thrown. - ANSFalse. Explicit upcasts are never a problem, and because of this, it's not even necessary to make them explicit - the Java compiler will do it for you implicitly. Explicit downcasts can sometimes cause a ClassCastException to be thrown, however. Java enums can be used with switch statements - ANSTrue Calling the values() method on an enum type will return an ArrayList of the enum's possible values - ANSFalse - it will return an Array, not an ArrayList. Assume we have access to an enum called Day that has the values SUN, MON, TUE, etc. The following statement will run successfully: Sln("Day: " + Day.MON ); - ANSTrue Fields inside a static nested class cannot be accessed by their outer class. - ANSFalse - privacy is not respected between the two classes Every inner class object must be connected to an outer class object - ANSTrue String[] is a subtype of Object[] - ANSTrue ArrayList<String> is a subtype of ArrayList<Object> - ANSFalse public class Container { ... } If MyObject is an interface, the program will not compile - ANSFalse public class Container { ... } If MyObject is a class then MyObject cannot be passed in for T - ANSFalse - MyObject or any subtype of MyObject can be used as the generic type for this class. The following method will compile but throw an exception at runtime (assuming the existence of the Food class). public void someMethod (ArrayList foodList <? extends Food> foodList) { foodL(new Food()); } - ANSFalse - it won't even compile. A collection cannot be added to if its generic type uses the ? extends operator. Putting final before a method means it cannot be overloaded - ANSFalse - it means that the method cannot be overridden Suppose the following declaration and instantiation is made: final int x = 8; Given this declaration, the following code fragment will NOT compile, but will throw an exception at runtime: int y = 21; x = (y + 7) / 4; - ANSFalse - it will not compile. Putting final before a method means any instance variable instantiated within it cannot have their value modified. - ANSFalse - it means that the method cannot be overridden. A class that has been declared final cannot have any subclasses. - ANSTrue Your friend is writing a name comparator for the Dog class, but is having some trouble. Here is the code for the comparator: public class DogNameComparator implements Comparator { public int compare(Dog other) { return Name().compareTo(Name()); } } Assuming that the Dog class has a getName method, what is the problem with the comparator shown above? - ANSThere is no current object "this", and the compare method is supposed to take two parameters. It looks like your friend got a bit confused between the compare() method and the compareTo() method. Here's what the correct implementation would look like: public int compare(Dog one, Dog two) { return Name().compareTo(Name()); } a. for (int i = 0; i < n; i++) { for (int j = i; j > n; j++) { Sln("hello world"); } } - ANSThe time complexity is actually O(n) here because the inner loop never runs. Notice that the inner loop begins at j = i, and its condition is j > n. However, if you look at the outer loop header, you can see that i will never be greater than n, so the condition j > n will never be satisfied. for (int i = 1; i < n; i*=2) { for (int j = i; j < n; j++) { Sln("hello world"); } } - ANSThe time complexity in this example is actually O(n*log(n)). While the outer loop depends on n, it depends logarithmically on n, not linearly. The inner loop depends linearly on n. Together, these two facts yield an asymptotic complexity of O(n*log(n)) for (int i = 0; i < n; i++) { S("hello"); } for (int j = 0; j < n; j++) { S("world"); } - ANSThe time complexity in this example is just O(n). While both loops depend linearly on n, they are not nested loops. Since they run completely independently of one another, we should add their individual time co
Written for
- Institution
- CMSC 132
- Course
- CMSC 132
Document information
- Uploaded on
- July 3, 2024
- Number of pages
- 8
- Written in
- 2023/2024
- Type
- Exam (elaborations)
- Contains
- Questions & answers
Subjects
-
cmsc132 exam 1 questions with 100 correct answer