Chapter 1 : Basic Concepts Flashcards

Chapter 1

1
Q

A file named Animal2.java class has the following code in it:

1     public class Animal {
2        private String name;
3    }
4    public class Animal2 {}

a) Line 1 does not compile
b) Line 4 does not compile
c) Exception thrown
d) Compiles fine

A

a)

You can have, at most, one public class in the *.java file and it must be named exactly the same as the file.

It is NOT required to have a public class in the file. Any number of package-protected classes can be defined. For example:

5   class Animal3{}
6   class Animal4{}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which lines in the below program can be removed safely without altering its behavior?

import java.lang.System;
import java.lang.*;
import java.util.Random;
import java.util.*
public class ImportExample {
    public static void main(String[] args) {
        Random x = new Random();
        System.out.println(x.nextInt(10));
    }
}
A

Lines 1, 2, and either 3 or 4

java.lang is imported automatically

Lines 3 or 4 import Random, therefore only one of them is needed. Although, best practice is to only import that which is needed in the program (line 3 is better)

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

A program has a Date variable. The class would not compile if the following import are declared:

import java.util.Date;
import java.sql.Date;

The following does not compile either

import java.util.;
import java.sql.
;

Why does the following compile?

import java.util.Date;
import java.sql.*;

A

Because explicit imports have precedence over ones with wildcards

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

A *.java class has the following code in it. Does it compile? If no, why not?

public class LineNumbers {
public void method(ArrayList list) {
   if (list.isEmpty()) { System.out.println("hi"); }
} else { System.out.println("bye");
} } }
A

It is missing an import statement for ArrayList

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

Does the following class compile? If not, why not?

public class Chicken {
   int eggs = 0; 
   public void Chicken(int eggs) {
       this.eggs = eggs;
   }
   public static void main(String[] args) {
      Chicken myChicken = new Chicken(4);
}
A

There is no constructor named Chicken(int eggs).

The class defines a method called Chicken(int eggs) which is not a constructor due to the return type void. Constructors do not have a return type.

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

What will this program print?

public class Junk {

{name = "Mary";}
String name = "Kate";
    public static void main (String[] args){
        Junk x = new Junk();
        String name = "Lynn"
        System.out.println(x.name);
    }
}
A

Kate.

Static initializers and field initializers run in the order in which they appear in the source code.

The line with “Lynn” is setting a local variable that happens to be named the same as the instance variable.

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

Name all Java primitives and the values they can hold

A
boolean    true/false
byte          8-bit integral value
short         16-bit integral value
int             32-bit integral value
long          64-bit integral value
float          32-bit floating-point value
double      64-bit floating-point value
char          16-bit Unicode value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which of the following does not compile?

1) double a = 1000.00
2) double b = 1000.00

3) double c = 1000_.00
4) double d = 1_0_0_0.0_0

A

1,2,3

Underscores can only exist between digits (not at either extreme or next to a decimal point)

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

In the following program, which is true?

1    public class Test {
2     public static void main (String args[]) {
3        long first = 4_000_000_000;
4        long second = 4000000000L;
5        System.out.println(first + second);
6      }
7   }

a) Line 3 does not compile
b) Line 4 does not compile
c) 8000000000 is printed
d) 8_000_000_000 is printed

A

a)

Line 3 does not compile because the literal is too large to represent an integer. Numerical literals are, by default, integers. An “L” or “l” at the end of the literal signals the literal should be treated as long instead.

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

Which of the following lines do not compile?

1    boolean b1, b2;
2   String s1 = "1", s2;
3   double d1, double d2;
4   int i1; int i2;
5   int i3; i4;
A

3 & 5

Line 3 : multiple variables can be declared and initialized in the same statement if the type appears only once at the beginning, and each variable being declared is separated by a comma.

Line 5 : those are 2 separate statements and i4 is not a valid statement (missing type)

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

Does line 1 do the same thing as line 2?

1 int a, b, c = 0;
2 int a = 0, b = 0, c = 0;

A

No

Line 1 only initializes variable c.

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

3 rules for naming variables, methods, classes, and fields

A

1) Must begin with a letter or the symbols $ or _
2) Subsequent characters can be numbers (as well as letters and the symbols $ or _)
3) Cannot be one of the all-lower-case reserved words in Java (int is reserved, INT is not but is bad design to use it as a variable name)

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

Given the following code:

1    import java.util.Date;
2     public class Junk {
3       static Date x;
4       public static void main (String[] args) {
5            Date x;
6            Date y = x;
7            System.out.println(y);
8      }
9   }

a) Line 5 does not compile
b) Line 6 does not compile
c) Line 7 does not compile
d) Exception thrown
e) null is printed

A

b)

x has not been initialized. Local variables must be initialized before they can be used.

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

A file named Animal2.java class has the following code in it:

1     class Animal3 {
2        private String name;
3    }
4    class Animal4 {}

a) Line 1 does not compile
b) Line 4 doe not compile
c) Exception thrown
d) Compiles fine

A

d)

It is NOT required to have a public class in the file. Any number of package-protected classes can be defined and none of them have to have the same name as the file.

The rule is that IF there is a public class in the file it has the to be the only one and must match the name of the source code file.

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

What are the default field values for the types :

boolean
byte, short, int, long
float, double
char
Any object
A
boolean = false
byte, short, int, long = 0
float, double = 0.0
char = '\n0000' (NUL)
Any object = null
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the scope of the following categories of variables:

a) Local variables
b) Instance variables (aka fields)
c) Class variables (aka static variables)

A

In scope…
Local = from declaration until the end of block
Instance = from declaration to garbage collection
Class = from declaration until end of JVM run

17
Q

Mandatory order of class elements :

class declaration
import statements
package declaration
A

PIC

Order is (P)ackage, (I)mport, (C)lass

None of the above are required to be present but if they are provided they MUST be in the order above.

18
Q

What is does System.gc() do?

A

It suggests to Java that it is a good time to run the garbage collector, however Java is not guaranteed to actually run garbage collection.

19
Q

For the following code, choose all that may apply:

1    public class Finalizer {
2        protected void finalize() {
3             System.out.println("Calling finalize");
4        }
5        public static void main(String[] args) {
6             Finalizer x = new Finalizer();
7        }
8    }

a) Line 2 does not compile
b) Line 6 does not compile
c) “Calling finalize” gets printed after line 6
d) Nothing gets printed

A

c & d

Garbage collection is not guaranteed to run after line 6 therefore the finalize() method may not be called for object x. The JVM may end its run without ever calling gc.

20
Q

For the following code, choose all that apply:

1     import java.util.*
2    public class Finalizer {
3       private static List objects = new ArrayList();
4       protected void finalize() {
5           if (objects != null) objects.add(this);
6           System.out.println("Revived");
7       }
8       public static void main(String[] args) {
9            Finalizer x = new Finalizer();
10            x = null;
11            Finalizer.objects = null;
12       }
13  }

a) “Revived” gets printed 0 times
b) “Revived” gets printed 1 time
c) “Revived” gets printed 2 times

A

a & b

a = Garbage collector is not guaranteed to run before JVM ends run
b = If gc gets called after line 7 then “Revived” will be printed and object x will not be destroyed because it was kept alive by adding to the objects List in line 4.
NOT c = After line 8, if gc were to be called, then finalize() does not get called on x again. Java knows the finalize() method already ran once and skips it. finalize() will never run more than once in any given object.

21
Q

Which of the following statements compile? Choose all that apply.

a) long a = 3.1f;
b) double b = 5L;
c) byte c = 3_000_000;
d) float d = 3.5;
e) short e = 100;
f) double f = 3.1f;
g) int g = 5L;

A

b), e), f)