Intro To Java Flashcards

1
Q

How do we execute a Java Program? Let’s say we have a program called Hello.java

A

Go to the command line, and navigate to your directory. You will then type

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

If you have a file in an IDE and want to run it using the command line, will we get any errors?

A

An IDE adds many files into the folder. If you try and run it within the folder that the IDE creates, it won’t work because of the package that the IDE creates.

You should always check your code in the command line.

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

What are the two ways of adding comments in Java?

A

Multi-line commenting is done with /* */

Same line commenting is done with //
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do we run the documentation in Java? Let’s say that we have a program called Hello.java

A

Like always, we first start by compiling:

javac Hello.java
javadoc Hello.java
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the primitive data types in Java?

A

boolean
char
byte
short (signed integer)
int (signed integer)
long (signed integer)
float (floating point)
double (floating point)

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

How is an integer declared in Java?

A

int exampleVariable = 409;

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

What is the difference between automatic type casting (widening) and explicit type casting (narrowing)?

A

Widening(Automatic): assigns a smaller type to a larger type (int -> double)

int intNum = 25;
double doubNum = intNum; //auto cast

Narrowing (Explicit): assign a larger type to a smaller type. (double -> int)

double doubNum = 25.5;
int intNum = (int)doubNum; //explicit cast
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we print out values in Java?

A
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the main() method?

A

Each class has its own main. Main can be thought of as an entry point.

Execution starts with the main. But just because a class doesn’t have a main doesn’t mean it won’t be used.

For assignments in this class, it is recommended that you use the main method.

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

What is the problem with this code?

public class Creature {
    public static void main(String[] args){
        System.out.println("This is a placeholder for Creature"+ args [0]);
    }
}
A

It can only handle the case that the user only enters in one argument. For example

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

For the following code, how would we throw an exception?

public class Creature {
    public static void main(String[] args){
        if(args.length == 0){
            \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_
        } else {
            System.out.println("This is a placeholder for Creature" + args[0])
        }
    }
}
A
public class Creature {
    public static void main(String[] args){
        if(args.length == 0){
            throw new IllegalArgumentException("You must supply a command-line argument");
        } else {
            System.out.println("This is a placeholder for Creature" + args[0])
        }
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is another name for data members and member functions?

A

Another name for data members is fields

Another name for member functions is methods

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

In Java is String a primitive function?

A

No, String is a class. Classes in Java start with capital letters.

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

What are some Non-Primitive Data Types in Java?

A

String
Array
List
Set
Stack

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

In Java, what convention do we use to name Classes and how does that differ from naming methods and variables?

A

In Java, we captialize the name of Classes, but we name methods and variables with lowercase.

We also use camel case for both.
ClassName
methodName

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

For the following access modifiers, what are their levels of visibility?
public
protected
default
private

A

public: Class, Package, Subclass, World
protected: Class, Package, Subclass, World
default: Class, Package
private: Class

17
Q

What is inclusion?

A

Inclusion is when you put one class inside another class.
(For now avoid this)

18
Q

Where are getter and setter functions placed (public or private?). What are the naming conventions for getter and setter functions?

A

Getter and setter functions are often placed in public.
The naming convention is to name getters with get in front and setters with set in front.

Example:
public class Creature {
    public static void main(String[] args){
        Animal myPet = new Animal();
        mypet.setAnimalType("mouse");
    }
    
}

class Animal {
    private String animalType;
    public void setAnimalType(String arg) {
        animalType = arg;
    }
    public String getAnimalType(){
        return animalType;
    }
}
19
Q

Consider the following code Fill in the blank so that we can retrieve the animal kingdom:

public class Creature {
    public static void main(String[] args){
        Animal myPet = new Animal();
        mypet.setAnimalType("mouse");
    }
   System.out.print(\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_); 
}

class Animal {
    private String animalType;
    public void setAnimalType(String arg) {
        animalType = arg;
    }
    public String getAnimalType(){
        return animalType;
    }
    public static String getKingdom(){
        return kingdom;
    }
}
A
public class Creature {
    public static void main(String[] args){
        Animal myPet = new Animal();
        mypet.setAnimalType("mouse");
    }
   System.out.print("This animal belongs to "+ Animal.getKingdom()); 
}

class Animal {
    private String animalType;
    public void setAnimalType(String arg) {
        animalType = arg;
    }
    public String getAnimalType(){
        return animalType;
    }
    public static String getKingdom(){
        return kingdom;
    }
}

Note that we set the value of kingdom to be the same for all animals.

20
Q

What is UML. What does it help us do?

A

UML stands for Unified Modeling Language. It provides a standard way to visualize elements of a system design.

It allows the visualization of structures, behaviours and interactions.

21
Q

Assume that we have a simple class named Person. How would we define private members name (a string) and age, and age (an integer).

A
public class Person {
    // Fields (data members) - private by default
    private String name;
    private int age;
		...
22
Q

In the following sample code, write code for a consructor that takes in two arguments, name and age, and then assigns them to the members name and age.
~~~
public class Person {
// Fields (data members) - private by default
private String name;
private int age;

~~~

A
public class Person {
    // Fields (data members) - private by default
    private String name;
    private int age;
    // Constructors - initialize the object
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
...
23
Q

Write a getter function called getName which retrieves the value for a variable called “name”. Will this method be public or private?

A

The method will be public:

    public String getName() {
        return name;
    }
24
Q

Write a setter method for updating the value of the private field ‘name’. In setter functions why is the “void” required?

A

The void keyword is used because it doesn’t return any value.
~~~
public void setName(String name) {
this.name = name;
}
~~~

25
In a UML diagram what does the top, middle and bottom section represent? What does the - sign indicate? What does the + sign indicate? What does an underline indicate?
The top is for the class name The middle is for the class fields The bottom is for class methods The - sign indicates that it is a field The + sign indicates that it is a method. An underline indicates that it is static
26
In the following program, in UML format write out the class fields section. ``` public class Animal { private String animalType; private static String kingdom = "animalia"; public void setAnimalType(String arg){ animalType = arg; } public String getAnimalType(){ return animalType; } public static String getKingdom(){ return kingdom; } } ```
-animalType: String -kingdom: String = "animalia" (note that this is underlined)
27
In the following program, in UML format write out the class methods section. ``` public class Animal { private String animalType; private static String kingdom = "animalia"; public void setAnimalType(String arg){ animalType = arg; } public String getAnimalType(){ return animalType; } public static String getKingdom(){ return kingdom; } } ```
+setAnimalType(arg:String) +getAnimalType(): String +getKingdom(): String
28
In Java, there is a standard library containing a Date class. Suppose we want to construct a new object called birthday.
``` Date birthday = new Date(); ```
29
Is the variable deadline an object? ``` Date deadline; ```
No, deadline doesn't refer to any object.The variable deadline is not an object.
30
What is happening in the following code? (What is new Date() doing, and what is happening with the deadline variable?) ``` Date deadline = new Date(); ``` | This has something to do with referencing.
The expression **new Date()** makes an object of type **Date**, and its value is a reference to that newly created object. That reference is then stored in the **deadline** variable.
31
Java object variables behave like what in C++? What is this Java code in C++ ``` Date birthday; // Java ```
You should think of Java object variables as object pointers in C++. ``` Date* birthday; //C++ ``` | Many people mistakenly believe that Java objects behave like references