MOOC Week 4 Flashcards

1
Q

What is a java constructor? What do its parametres do?

A

A special method used to initialise objects, the constructor is called when an object of a class is created. Parametres in a contructor can initialize attributes.

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

What is static? Why do we use it?

A

It means the object or method belongs specifically to the class. This is to ensure it cannot be used to access any variables that belong to objects. Also its ensures method implementations cannot be swapped or overide at runtime.

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

What is enum?

A

Stands for enumerator. Is is a special data type that allows a variable to be a set of predefined constants

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

What is the purpose of the this keyword?

A

Reference to the current object.

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

What is the purpose of the public keywords.

A

Access modifiers. Restricts the scope or accessibility of a class, constructor, variables, methods, and data members. It allows JVM to invoke it from outside the class, meaning any object can use the main method.

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

What is the purpose of the private keyboard and give 2 of its advantages.

A

Private access modifier that ensures methods, variables and classes can only be accessed in the class in which they were declared or by inheriting parent classes. Private declared methods cannot be override. Private access modifier hides classes from other classes of the same package.

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

What is the purpose of the JVM?

A

The engine that drives java code, specifically interpreting byte code into machine code. Java is called platform independent because of byte code. The JVM is responsible for allocating and deallocating memory space.

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

What is the main method?

A

A identifier the JVM looks for as the starting point of a program.

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

What does String[] args mean in the main method?

A

An array, where every element is at least a string, which will be known as arguments. Meaning it stores command line arguments.

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

What is the toString() method?

A

toString() method is used to return the string representation of a object.

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

When is setVariable naming convention used?

A

When an methods only purpose is to set a value to the instance variable.

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

Is a string an object? Can objects be added to a list?

A

Yes

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

How to read from file? Example and what packages do you need to import.

A

import java.util.Scanner;
import java.nio.file.Paths;

public class Read {
    public static void main(String args[]) {
        try (Scanner scanner = new Scanner(Paths.get("file1.txt"))) {
        while(scanner.hasNextLine()) {
            String row = scanner.nextLine();
                System.out.println(row);
            }
        } catch (Exception e) {
            System.out.print("Error: " + e.getMessage());
        }
}  }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly