Exam review Flashcards

(111 cards)

1
Q

T/F

In a class, all methods except constructors can be overloaded when needed.

A

False.

Constructors can be overloaded.

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

T/F

The signature of a method only includes the method name and its parameter types. Does not include the type returned.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
T/F
Overloading happens when two or more methods in the same class have the same method name
A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
T/F 
It is legal in java if 2 methods defined in the same class have the same name and parameter types by different return types
A

False

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

What is the value of str?

String str = “abc” + 4 + “fgh” + 5;

A

“abc4fgh5”

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

System.out.println (“"http:\www.queensu.ca"”); will display:

A

“http:\www.queensu.ca”

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

T/F

Static methods can’t call non-static methods.

A

True

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

T/F

Static methods can change values of non-static instance variables if they are in the same class.

A

False

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

T/F

Static methods can’t invoke static methods in the same class.

A

False

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

T/F

Static methods can always use “this” operators

A

False

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

T/F

A constructor has no type returned, not even void.

A

True

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

T/F

A constructor is a special type of methods that can be used to initialize the instance variables for an object.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
T/F
Every class can have at most one constructor and sometimes a class does not need a constructor when there is no need to initialize instance variables.
A

False

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

T/F

A constructor must have the same name as the class.

A

True

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

T/F

It must be declared outside of a class.

A

False

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

T/F

It must be in its own class.

A

False

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

T/F

It must have the keyword static.

A

True

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

T/F

It must have the keyword final

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
What does the following code print?
public class Car {
    private String name;
    public Car(String name) {
        this.name = name;
    }
    public static void main(String[] args) {
        Car myFirstCar = new Car("Ford Fusion");
        Car mySecondCar = new Car("Ford Fusion");
        Car myThirdCar = new Car("Mazda 3");
        if (myFirstCar != mySecondCar)
            System.out.println("My first car and second are not same.");
        else if (mySecondCar == myThirdCar)
            System.out.println("My second car and third are same.");
        else 
            System.out.println("My second car and third are not same.");
    }
}
A

My second car and third are not same.

BECAUSE MyFirstCar and MySecondCar do not reference the same object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
What is the output of the following code
int vehicleType = 2;
switch (vehicleType) {
case 1:
    System.out.println("Passenger car.");
case 2:
    System.out.println("Bus.");
case 3:
    System.out.println("Truck.");
default:
    System.out.println("Unknown vehicle class!");
    break;
}
A

Bus.
Truck.
Unknown vehicle class!

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

T/F
“Java has a two-step translation process to convert Java source programs to machine language. The output of the first step is called byte-code.”

A

True

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

What is the order that variables can be assigned in?

A

byte -> Short -> int -> long -> float ->double,

And Char hops in before int, but it cannot be a byte or short

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

T/F
“In procedural programming, procedures and data are clearly separated from each other, so it is easier to maintain and reuse code in procedural programming than in object oriented programming.”

A

False

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

What are the major components of a class?

A

Instance variables (attributes) and methods

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
``` public class Figure { public void display() { System.out.println("Figure"); } } public class Rectangle extends Figure { public void display() { System.out.println("Rectangle"); } } public class Box extends Rectangle { public void display() { System.out.println("Box"); } } //upcasting example public class Main { public static void main(String[] args) { Figure f = new Figure(); Rectangle r = new Rectangle(); Box b = new Box(); f.display(); f = r; f.display(); f = b; f.display(); } } ```
Figure Rectangle Box
26
T/F getClass should be used because it works correctly (consistently) in cases where one object being compared is derived from the other object.
True
27
T/F Both getClass and instanceOf work well for defining the equals method. There is NO reason for preferring getClass over instanceOf.
False
28
T/F | Using getClass is more efficient at runtime than instanceOf
False
29
T/F | You should use instanceOf, not getClass when overriding the equals method.
False
30
Say you want to access a variable in a base class, from a derived class, but it is marked private. Which statement is true?
You should use an accessor or mutator defined in the base class to access or change the private variable, respectively.
31
``` public class First { public First() { System.out.println("Base class"); } } ``` ``` public class Second extends First { public Second() { super(); System.out.println("Child class"); } } ``` ``` public class Main { public static void main(String[] args) { Second s = new Second(); } } ```
Base class Child class
32
``` T/F Java has a program called Javadoc that can (when properly configured) extract information such as class headings, headings for methods, and instance variable information to produce documentation. ```
True
33
``` public class First { public void Print() { System.out.println("Grandparent class"); } } ``` ``` public class Second extends First { public void Print() { System.out.println("Parent class"); } } ``` ``` public class Third extends Second { public void Print() { super.super.Print(); System.out.println("Child class"); } } ``` ``` public class Main { public static void main(String[] args) { Third c = new Third(); c.Print(); } } ```
The code has a compilation error; it cannot be compiled correctly.
34
T/F | Overriding is another term for overloading
False
35
T/F | Overriding can only be done together with inheritance; it cannot be done without inheritance.
True
36
T/F | Overloading can only be done together with inheritance; it cannot be done without inheritance.
False
37
``` T/F In Java, a final class (a class marked by the keyword final) can be always inherited by another class. ```
False
38
``` T/F In all versions of Java, when a method in a derived class overrides a method in a base class, the signature (method name and parameter types) and return type of the overriding method must be exactly same as those of the overridden method, with no exception. Java never allows the return types of these two methods to be different. ```
False
39
Java uses early binding, not late binding, for methods marked as.....
Final, static, private
40
``` public class First{ public int fun(int i){ return i+3; } } ``` ``` public class Second extends First{ public double fun(double i) { return i + 3.3; } } ``` ``` public class Main { public static void main(String args[]) { Second s = new Second(); int input = 3; System.out.println(s.fun(input)); } } ```
6
41
What are the three main programming mechanisms that constitute object-oriented programming?
Encapsulation, inheritance, polymorphism
42
Define encapsulation
``` A mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. The variables of one class will be hidden from other classes. To do this: 1) Mark variables as 'private' 2) Provide public getter and setters to modify and view the variables. ```
43
What is polymorphism?
Polymorphism allows changes to be made to method definitions in the derived classes, and have those changes apply to the software written for the base class.
44
``` T/F Protected instance variables in a base class are accessible by name in a derived class, even if the derived class and the base class are not in the same package ```
True
45
T/F Protected instance variables in a class ToyClass can be accessed by name in another class, say AnotherClass, if ToyClass and AnotherClass are in the same package.
True
46
``` T/F Private instance variables in a base class are accessible by name in a derived class. ```
False
47
``` T/F Public instance variables in a base class are accessible by name in a derived class. ```
True
48
T/F In Java generic classes, the parameter type T can be used with the "new" keyword to create an object. The following code is legal: T myObject = new T();
False
49
``` T/F When a generic class has one type parameter, the contructor(s) of this class can still have more than one input argument ```
True
50
T/F | An interface can contain constant instance variables
True
51
In some situations, an interface can contain non-empty methods(i.e. methods with bodies)
False
52
When you define a concrete class that implements an existing interface, in that concrete class you must define all methods declared in the interface with non-empty bodies
True
53
Both interfaces and abstract classes can declare empty methods
True
54
T/F | Outer and inner classes only have access to each other's public members
False
55
T/F One of the most popular reasons of defining inner classes is to use them as helping classes to help implement some methods in the outer class.
True
56
T/F | Outer and inner classes only have access to each other's public and protected members.
False
57
T/F | Outer and inner classes do not have access to each other's private members
False
58
When defining an inner class to be a helping class for an outer class, the inner class itself must be marked as a public class.
False
59
``` T/F Java do not allow an instance/object of an abstract class to be created. ```
True
60
If a method does not catch a checked exception, then it must at least warn programmers that any invocation of the method might possibly throw an exception. This process is implemented with:
Throws clauses
61
In Java, a class, say A, can be a subclass of multiple number of classes, say B and C, and at the same time the class A can implement multiple numbers of interfaces.
False
62
What is a collection
A collection is a data structure for holding elements; e.g., a set or a list is a collection Specifically, a Collections framework that includes a number of interfaces, abstract classes, and concrete classes
63
``` T/F Classes that implement Set allow an element in the class to occur more than once ```
``` False Do not allow and element in the class to occur more than once ```
64
T/F | Methods that are optional in Collection interface are required in the Set interface
True
65
What are the pros and cons of a larger initial HashMap capacity?
Results in faster performance but uses more memory
66
What is a load factor?
A number between 0 and 1 that specifies the percentage of elements in a hash table compared to its capacity.
67
What happens if the number of elements added to the hash table exceeds the load factor?
The capacity of the hash table is automatically increased
68
What is the purpose of the Map interface?
It represents a mapping between a key and a value, . Used to perform lookups by keys or when someone wants to retrieve and update elements by keys.
69
What is an iterator?
An object that is used with a collection to provide sequential access to the collection elements.
70
What is the purpose of an iterator?
Allows examination and possible modification of the elements.
71
What does an iterator do?
Imposes an ordering on the elements of a collections even if the collection itself does not impose any order on the elements it contains.
72
What does an iterator do if a collection already imposes an ordering on its elements?
The iterator will use the same ordering
73
How do you use a for loop to cycle through each element in a collection?
for (String anEntry : myHashSet) {}
74
What happens if you try to modify a collection inside a for-each loop?
It will throw a ConcurrentModificationException | An iterator can modify a collection
75
If a list has n elements, how are the elements numbered and how many cursor positions are there?
Elements indexed from 0 to n-1 | n+1 cursor positions
76
How can data be inputted and outputted from main memory?
- GUI, like assignment 3 - : Keyboard: Scanner, like assignment 1&2 - Files - Voice
77
What is a basic way of reading data?
Stream! Can flow from keyboard or from a file
78
What happens to output streams connected to files?
They are usually buffered to increase efficiency. When enough data accumulates, or when the method flush() is invoked, the buffered data is written to the file all at once.
79
T/F | Java binary files are portable, i.e., can be moved from one computer to another
True, but they can only be read by a java program
80
Why is it best to store the data of only one class type in any one file?
Storing objects of multiple class types or objects of one class type mixed with primitives can lead to loss of data.
81
``` T/F A class is serializable if the classes for all instance variables are also serializable for all levels of instance variables within classes ```
True
82
What is a pattern?
Design outlines that apply across a variety of software applications
83
What is a container?
``` A container is a class or other construct whose objects hold multiple pieces of data Array, linked list, etc ```
84
What is the Model-View-Controller pattern?
A way of separating the I/O task of an application form the rest of the application
85
How does Model-View-Controller work?
Model: Performs the heart of the application: View: Displays (outputs) a picture of the model's state Controller: Relays commands from the user to the model
86
What does the Adaptor pattern do?
Transforms one class into a different class without changing the underlying class by adding a new interface.
87
What is a design pattern?
A design pattern is a general solution to a common problem in a context. Like a recipe
88
List some good design principles
* Separate what changes from what does not * Encapsulate what varies behind an interface * Loosely couple objects that interact * Classes should be open for extension * Each class should have one responsibility
89
What does a singleton pattern do?
Restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine
90
What is the pitfall of design patterns?
Overuse can lead to code that is over-engineered. Always go with the simplest solution that does the job and introduce patterns only where the need emerges.
91
What are the three types of data?
1) Unstructured (Video, audio, text) 2) Semi-structured (Webpages) 3) structured (Relational databases)
92
What allows SQL commands to be inserted into Java code?
``` Java Database Connectivity (JDBC) Database System (DBS) must be installed ```
93
What are the steps to accessing a database?
1) Load the driver 2) Connect to database using Connection String 3) Issue SQL commands to access or manipulate the database 4) Close the connection when done.
94
T/F | Without explicit type-casting, you can assign the value of a boolean variable to an int variable
False
95
In java, variables of type String are immutable objects
True
96
Efficiency is lost in importing an entire package (e.g., by using "*") instead of importing only the specific classes in that package
False There is no additional overhead for importing the entire package It just gives the code better readability if you know exactly which packages are being used
97
Not including the break statements within a switch statement results in a syntax/compilation error
False
98
In Java, when you invoke a method, there must be exactly the same number of arguments in () as there are parameters in the method definition
True
99
The number of entries that may be added to a HashMap object is limited to what is specified as the initial capacity in the crunstructor
False | If the number of elements added to the hash table exceeds the load factor, then the capacity is increased.
100
You may use methods of the Math class without an import statement
False
101
Wrapper classes are provided for all primitive Java types except boolean
False
102
A constructor for a derived class must start with an invocation of a constructor from the base class
True
103
Private methods of the base class cannot be accessed by name by derived classes
True
104
The stream classes for processing binary files are ObjectInputStream and ObjectOutputStream
True
105
Java does not require that a variable be declared before it is used within a program
False
106
If the final modfier is included before the definition of a method, the the method can still be redefined in a derived class
False
107
Java sometimes allows an instance of an abstract class to be instantiated
False
108
Java uses late binding with public and static methods but not with private or final methods
True
109
The method Clone() has one parameter and should return a copy of the calling object
False | Clone has no parameters
110
When an interface lists a method as "Optional" you often do not need to implement it when defining a class that implements the interface
False. When an interface lists a method as "optional", it will normally still be implements. But in some situations, a programmer can also choose to leave the method unsupported.
111
Exceptions that must follow the "Catch or Declare Rule" are often called unchecked exceptions
``` False The class Exception is checked. The class Error and RuntimeException are both unchecked ```