Provide three different primitive types.
Primitive Types: byte, short, int, long, float, double, char, boolean
The process of instantiating a class is called?
Instantiation
Explain the concept of Inheritance.
Inheritance allows a programmer to define a general class, and then later define more specific classes that share or inherit all of the properties of the more general class.
Write a method that reads a file and prints the first line.
Define a class that prints "Hello, World!" in the main method.
public class Prompt { public static void main(String[] args){ System.out.println("Hello, World!"); } }
Do you need to declare a constructor in order to compile a class?
No, every class comes with a default constructor. It has no parameters
@Testable annotates the unit test class
@Test annotates each test method
When declaring the visibility of a method or variable, what are the three main types that are used? Explain who is granted access.
public – grants access to everyone
protected – grants access to this class and
inherited classes
private – grants access to this class only
Suppose class A extends class B. How should class A invoke the constructor, access attributes, or call a method from class B.
super();
super.attribute;
super.method();
Write a method that will ask the user for their input.
Make sure to follow proper coding procedures.
Scanner scanner = new Scanner(System.it); System.out.print("Enter age: "); int age = scanner.nextInt();
How should you go about comparing two objects with different memory addresses?
Overriding and defining an equals method.
What is the main difference between primitive and reference types?
The basic difference is that primitive variables store the actual values, whereas reference variables store the addresses of the objects they refer to
Explain the difference between checked and unchecked exceptions.
Unchecked Exceptions: You are not explicitly required to handle an unchecked exception in any way
Checked Exceptions: The programmer must handle the exception in some way; otherwise, a compile error will occur.
WILD CARD
- 150 Points
Define the following as a class diagram (UML).
A pet usually has a name, age, and breed. You can usually get the Pet's attribute and adopt a Pet whenever, which also consists of renaming the Pet.
WILD CARD
+ 150 Points
Is converting a less complex type (small int) to a more complex type (large int) considered safe?
Yes
What is the difference between overloading and overriding?
When two or more methods in the same class have the same name but different parameters, it's called Overloading.
When the method signature (name and parameters) are the same in the superclass and the child class, it's called Overriding.
Suppose we have a Child class that Extends the Parent class. When we instantiate an instance of the child Class, can the variable be a type of the Parent class?
Parent parent1 = new Parent("Anakin"); Child child = new Child("Jon", 19); Parent parent2 = child;
Write a method that reads a file and prints the first line.
Make sure to follow proper coding procedures.
FileReader fileReader = new FileReader("file.txt"); BufferedReader reader = new BufferedReader(fileReader); String line = reader.readLine(); reader.close(); fileReader.close();
What are some constraints of utilizing abstract classes and interfaces?
You can only extend one abstract class and implement many interfaces.
When it comes to reading a file, we utilize the java.io.FileReader class and java.io.BufferedReader class. Explain the significance of the BufferedReader.
The java.io.FileReader is a little hard to use because it only supports reading characters (one at a time or in chunks). The java.io.BufferedReader provides a readLine() method that reads up to the next newline from the file.
Explain the concept/idea of encapsulation.
Encapsulation is the idea of locating
information and actions related to an object in
one place.
What makes an abstract class different from an interface?
An abstract class allows you to create functionality that subclasses can implement or override.
An interface only allows you to define functionality, not implement it.
Define the following as a class diagram (UML).
A class diagram that utilizes inheritance through abstraction or interfaces.
Throughout the semester, we utilized the keyword static multiple times. Define static and provide a side effect/outcome of utilizing it.
Static indicates to Java that only one instance of this item will ever exist and that it belongs to the class.
You can access it using class.staticMember instead of object.member (which means you need to initialize an instance of a class first before calling the method)