Object Oriented Design - Java - Midterm
What is a multi-threaded program?
A multi-threaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.
What are packages in Java?
A package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations ) providing access protection and name space management. A package equates to a folder (directory) in java. Packages can be nested.
What is the difference between processes and threads?
A process is an execution of a program, while a thread is a single execution sequence within a process. A process can contain multiple threads. A thread is sometimes called a lightweight process.
Can you access a non-static variable in static context?
A static variable in Java belongs to its class and its value remains the same for all its instances. A static variable is initialized when the class is loaded by the JVM. If your code tries to access a non-static variable, without any instance, the compiler will complain, because those variables are not created yet and they are not associated with any instance.
What is the difference between static and non-static variables?
A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.
What is an abstract class?
Abstract classes cannot be instantiated and are either partially implemented or not at all implemented. An abstract class can contain one or more abstract methods which are simply method declarations without a body.
What is abstraction?
Abstraction is the process of separating ideas from specific instances and thus, develop classes in terms of their own functionality, instead of their implementation details. Java supports the creation and existence of abstract classes that expose interfaces, without including the actual implementation of all methods. The abstraction technique aims to separate the implementation details of a class from its behavior.
What are the access levels in java and who can access them?
Access Levels Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N
In JavaFX clicking a button generates a(n) __________ event.
ActionEvent
What invokes a thread's run() method?
After a thread is started, via its start() method of the Thread class, the JVM invokes the thread's run() method when the thread is initially executed.
Which method must be implemented by all threads?
All tasks must implement the run() method.
What is the difference between an interface and an abstract class?
An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation.
When is the throw keyword used?
An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using the throw keyword.
What is an Interface?
An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
What is type casting?
Type casting means treating a variable of one type as though it is another type.
What is the Factory OO design pattern?
Using a method to create objects without having to specify the exact class of the object to be produced.
What is a local variable?
Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.
How does a try statement determine which catch clause should be used to handle an exception?
When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exception is executed. The remaining catch clauses are ignored.
Can an interface extend another interface?
Yes, an interface can inherit from another interface. For that matter, an interface can extend more than one interface.
Every javafx.event.Event object has the ________ method.
getSource()
What is the difference between 'int' and 'Integer' in Java?
int is a primitive data type and Integer is a class
public class MyRunnable implements Runnable { public void run() { // some code goes here } } how to create a start a new thread?
new Thread(new MyRunnable()).start();
A Java class can implement _____ interface(s)
one or more than one.
To create an InputStream to read from a file on a Web server, you use the method __________ in the URL class.
openStream();
Given a method in a protected class, what access modifier do you use to restrict access to that method to only the other members of the same class?
private
Which method must be defined by a class implementing the java.lang.Runnable interface and serves as the path of execution for all threads?
run()
What method in the Thread class do you call to make the current thread cease executing for a specified amount of time?
sleep()
What is the name of the method used to initiate execution of a thread?
start()
[T/F] try statements can be nested?
True
What is true about abstract classes?
- A class containing abstract methods has to be an abstract class. - Abstract methods must be implemented in the derived class if the derived class is concrete. - An abstract class can have non-abstract methods. - A class must be abstract if it contains an abstract method.
Give some features of an interface?
- An interface cannot be instantiated - An interface does not contain any constructors. - All of the methods in an interface are abstract. - All of the methods in an interface are public by default.
List the Java primitive types.
- byte - char - short - int - long - float - double - boolean
When does an object becomes eligible for garbage collection in Java?
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is currently used.
What is constructor chaining and how is it achieved in Java?
A child object constructor always first needs to construct its parent. In Java it is done via an implicit call to the no-args constructor as the first statement unless there is an explicit call to one of the parent constructors as the first statement.
Composition means ______________.
A class contains a data field that references another object.
What is a constructor?
A constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class. The constructor is used to initialize an object.
How can a dead thread be restarted?
A dead thread cannot be restarted.
What is a final class?
A final class is created so the methods implemented by that class cannot be overridden. A final class can't be inherited.
A finally block is executed ______________.
A finally block is executed **whether an exception is thrown or not.**
What is the importance of the finally block in exception handling?
A finally block will always be executed, whether or not an exception is actually thrown. Even in the case where the catch statement is missing and an exception is thrown, the finally block will still be executed. Last thing to mention is that the finally block is used to release resources like I/O buffers, database connections, etc.
What can the 'super' keyword be used to do?
Call the super class's constructor and access the super class's member
What is a static variable?
Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.
What is object serialization?
Converting an object into a stream of bytes in order to store or transmit the object.
The wrapping up of data and functions into a single unit is called _______________.
Encapsulation
[T/F] A static method can refer to any instance variable of the class.
False
[T/F] An instance of a thread can be started twice.
False
[T/F] Each method in a class must have a unique name.
False
[T/F] Java does not allow a method with the same signature in a subclass as a method in the super class.
False
[T/F] The == operator can be used to compare the values of two String objects and the result is true if the Strings contain the same characters in the same order.
False
[T/F] The code in a thread that is different than the main/UI thread should directly manipulate user interface elements?
False
Which class contains the method for checking whether a file exists?
File
What is the default value of Float and Double data types in Java?
Float and Double are object types and variables declared of these type are initialized to null.
What is the default value of float and double data types in Java?
For float its 0.0f and for double it's 0.0d
Inheritance exhibits the "IS-A" relationship while composition exhibits the _______ relationship.
HAS-A
What exception is thrown by the read() method of InputStream class
IOException
What is function overloading?
If a class has multiple functions by the same name but different parameters, it is known as method or function overloading.
When is the throws keyword used?
If a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.
What is function overriding?
If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as method or function overriding.
When is the super keyword used?
If the method overrides one of its superclass's methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field or call one of the superclass's constructors.
How do you decide when to use ArrayList and LinkedList?
If you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList should be used. If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList should be used.
When are abstract methods used?
If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.
What is an instance variable?
Instance variables are variables within a class but outside any method that are part of object instances when they are created. The storage for instance variable is created when a new instance of an object is created.
Is it necessary that each try block must be followed by a catch block?
It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block or a finally block.
What is downcasting?
It is the casting from a general to a more specific type, i.e. casting down the hierarchy. In a UML diagram the more specific classes are below the the more general classes.
[T/F] The 'this' keyword is used to resolve conflicts between method parameters and instance fields/methods of the invoked class.
True
[T/F] When an instance of a class (an object) is specified as a parameter to a method a reference to the said object is passed to the method.
True
What is Encapsulation?
It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore, encapsulation is also referred to as data hiding. Encapsulation provides objects with the ability to hide their internal characteristics and behavior. Each object provides a number of methods, which can be accessed by other objects and change its internal data. In Java, there are four access modifiers: public, private, protected, and default. Each modifier imposes different access rights to other classes, either in the same or in external packages. Some of the advantages of using encapsulation are listed below: - The internal state of every objecte is protected by hiding its attributes. - It increases usability and maintenance of code,because the behavior of an object can be independently changed or extended. - It improves modularity by preventing objects to interact with each other, in an undesired way.
What is the difference between an interface and an abstract class?
Java provides and supports the creation both of abstract classes and interfaces. Both implementations share some common characteristics, but they differ in the following features: - All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and non-abstract methods. - A class may implement a number of interfaces, but can extend only one abstract class. - Abstract classes can implement interfaces without even providing the implementation of interface methods. - Variables declared in a Java interface are by default final. An abstract class may contain non-final variables. - Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public. - An interface is absolutely abstract and cannot be instantiated. Note: Java 8 allows interfaces to have default methods.
Does Java support multiple inheritance?
No, Java does not support multiple inheritance. Each class is able to extend only on one class, but is able to implement more than one interface.
What is the default value of an object reference declared as an instance variable?
Null, unless it is defined explicitly.
What restrictions are placed on method overriding?
Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.
Why are packages used?
Packages are used in Java in-order to: - prevent naming conflicts - to control access - to make searching/locating and usage of classes, interfaces, enumerations and annotations, etc., easier.
What is polymorphism?
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Polymorphism is the ability of programming languages to present the same interface for differing underlying data types. A polymorphic type is a type whose operations can also be applied to values of some other type.
Which keyword can protect a class in a package from accessibility by the classes outside the
The default keyword
What is the dot operator?
The dot operator (.) is used to access the instance variables and methods of class objects. It is also used to access classes and sub-packages from a package.
How is finally used under Exception handling?
The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.
What is a Class Variable?
These are variables declared within a class, outside any method, with the static keyword. The storage for them is created when the program is run and the classes are loaded.
What are the two ways in which a thread can be created?
Threads can be created by: - implementing Runnable interface - extending the Thread class
[T/F] A method in a class declared as static can only access static class members.
True
[T/F] A subclass is usually extended to contain more functions and more detailed information than its superclass.
True
[T/F] All interface methods must be declared as public when implemented in a class.
True
[T/F] An instance of the java.io.File class can be used to delete a file.
True
[T/F] Objects of a subclass can be assigned to a super class reference.
True
What method is available on every object instance in Java
equals()