Chapter 1 Flashcards

1
Q

What is the result of the following class? (Choose all that apply)

1: public class _C {
2: private static int $;
3: public static void main(String[] main) {
4: String a_b;
5: System.out.print($);
6: System.out.print(a_b);
7: } }

A

Compiler error on line 6.

Because the local variable a_b wasn’t initialized

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
What is the result of the following code?
String s1 = "Java";
String s2 = "Java";
StringBuilder sb1 = new StringBuilder();
sb1.append("Ja").append("va");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
System.out.println(sb1.toString() == s1);
System.out.println(sb1.toString().equals(s1));
A

true
true
false
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What is the output of the following code? (Choose all that apply)
1: interface HasTail { int getTailLength(); }
2: abstract class Puma implements HasTail {
3: protected int getTailLength() {return 4;}
4: }
5: public class Cougar extends Puma {
6: public static void main(String[] args) {
7: Puma puma = new Puma();
8: System.out.println(puma.getTailLength());
9: }
10:
11: public int getTailLength(int length) {return 2;}
12: }
A

The code will not compile because of line 7.

Cannot instantiate the type Puma

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

What is the output of the following program?

1: public class FeedingSchedule {
2: public static void main(String[] args) {
3: boolean keepGoing = true;
4: int count = 0;
5: int x = 3;
6: while(count++ < 3) {
7: int y = (1 + 2 * count) % 3;
8: switch(y) {
9: default:
10: case 0: x -= 1; break;
11: case 1: x += 5;
12: }
13: }
14: System.out.println(x);
15: } }

A

6

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

What is the output of the following code snippet?

13: System.out.print(“a”);
14: try {
15: System.out.print(“b”);
16: throw new IllegalArgumentException();
17: } catch (RuntimeException e) {
18: System.out.print(“c”);
19: } finally {
20: System.out.print(“d”);
21: }
22: System.out.print(“e”);

A

abcde

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

What is the result of the following program?

1: public class MathFunctions {
2: public static void addToInt(int x, int amountToAdd) {
3: x = x + amountToAdd;
4: }
5: public static void main(String[] args) {
6: int a = 15;
7: int b = 10;
8: MathFunctions.addToInt(a, b);
9: System.out.println(a); } }

A

15

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

What’s an object?

A

An object is a runtime instance of a class in memory. All the various objects of all the different classes represent the state of your program.

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

What are the members of the class?

A

Methods and variables.

Variables hold the state of the program, and methods operate on that state.

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

What’s method signature?

A

The full declaration of a method. For example:

public int numberVisitors(int month)

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

There are how many types of comments?

A
There are three types of comments in Java. The first is called a single-line comment:
// comment until end of line
A single-line comment begins with two slashes. Anything you type after that on the same line is ignored by the compiler. Next comes the multiple-line comment:
/* Multiple
 * line comment
 */
A multiple-line comment (also known as a multiline comment) includes anything starting from the symbol /* until the symbol */. People often type an asterisk (*) at the beginning of each line of a multiline comment to make it easier to read, but you don’t have to. 
Finally, we have a Javadoc comment:
 /**
 * Javadoc multiple-line comment
 * @author Jeanne and Scott
 */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can we have more than one class in a Java file?

A
Yes. You can even put two classes in the same file. When you do so, at most one of the classes
in the file is allowed to be public. That means a file containing the following is also fine:
1: public class Animal {
2: private String name;
3: }
4: class Animal2 {
5: }
If you do have a public class, it needs to match the filename. public class Animal2 would not compile in a file named Animal.java.

If there is just a class in the file, y, Java does not require that the class be public. For example, this class is just fine:

1: class Animal {
2: String name;
3: }

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

What are the commands to compile and execute a Java program?

A

javac Zoo.java

java Zoo

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

What is bytecode?

A

Bytecode consists of instructions that the JVM knows how to execute.

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

What is an array?

A

An array is a fixed-size list of items that are all of the same type.

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

What parameters list are accepted by the main method?

A

String[] args, String args[] or String… args;

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

What are packages?

A

These are logical groupings for classes. The import statement tells the compiler which package to look into find a class. This is similar to how mailing a letter works.

17
Q

Error message when we don’t import the package?

A

Compiler error: Random cannot be resolved to a type

18
Q

What’s an wildcard?

A

The * is a wildcard that matches all classes in the package. A shortcut to import all the classes in a package:
import java.util.*; // imports java.util.Random among other things.
It doesn’t slow down your program. The compiler figures out what’s actually needed.

19
Q

What is the one special package in the Java world?

A

There’s one special package in the Java world called java.lang. This package is special in that it is automatically imported. You can still type this package in an import statement, but you don’t have to.

20
Q

Examples of imports that will not work

A

For the classes Path and File:

import java.nio.file.*; //works

The other answer is to import both classes explicitly:
import java.nio.file.Files;
import java.nio.file.Paths;

import java.nio.*; // NO GOOD – a wildcard only matches class names, not "file.*Files"
import java.nio.*.*; // NO GOOD – you can only have one wildcard and it must be at the end
import java.nio.files.Paths.*; // NO GOOD – you cannot import methods only class names
21
Q

What happen if we import two classes with the same name?

A

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

Because there can’t be two defaults, the compiler tells you:
The import java.sql.Date collides with another import statement

22
Q

How to use two classes with the same name?

A
You can pick one to use in the import and use the other’s fully qualified class name (the package name, a dot, and the class name) to specify that it’s special. For example:
import java.util.Date;
public class Conflicts {
 Date date;
 java.sql.Date sqlDate;
}
Or you could have neither with an import and always use the fully qualified class name:
public class Conflicts {
 java.util.Date date;
 java.sql.Date sqlDate;
}
23
Q

How to use two classes with the same name?

A
You can pick one to use in the import and use the other’s fully qualified class name (the package name, a dot, and the class name) to specify that it’s special. For example:
import java.util.Date;
public class Conflicts {
 Date date;
 java.sql.Date sqlDate;
}
Or you could have neither with an import and always use the fully qualified class name:
public class Conflicts {
 java.util.Date date;
 java.sql.Date sqlDate;
}
24
Q

How to compile and run a classes that belongs to different packages?

A

Create the two files:
■ C:\temp\packagea\ClassA.java
■ C:\temp\packageb\ClassB.java

Then type this command:
cd C:\temp

To Compile
Type this command:
javac packagea/ClassA.java packageb/ClassB.java

 If the command does work, two new fi les will be created:
packagea/ClassA.class and packageb/ClassB.class.

To Run
Type this command:
java packageb.ClassB

25
Q

What’s a JAR file?

A

A JAR file is like a zip file of mainly Java class files.

26
Q

How to specify the location of other files explicitly using a class path?

A

java -cp “.;C:\temp\someOtherLocation;c:\temp\myJar.jar” myPackage.MyClass

The dot indicates you want to include the current directory in the class path. The rest of the command says to look for loose class fi les (or packages) in someOtherLocation and within myJar.jar. Windows uses semicolons to separate parts of the class path; other operating systems use colons.
Finally, you can use a wildcard (*) to match all the JARs in a directory. Here’s an example:
java -cp “C:\temp\directoryWithJars*” myPackage.MyClass
This command will add all the JARs to the class path that are in directoryWithJars. It won’t include any JARs in the class path that are in a subdirectory of directoryWithJars.

27
Q

What’s a constructor?

A

It’s a special type of method that creates a new object

public class Chick {
 public Chick() {
 System.out.println("in constructor");
 }
}

There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there’s no return type.

28
Q

What’s a constructor?

A

It’s a special type of method that creates a new object

public class Chick {
 public Chick() {
 System.out.println("in constructor");
 }
}

There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there’s no return type.

The purpose of a constructor is to initialize fi elds, although you can put any code in there. Another way to initialize fields is to do so directly on the line on which they’re declared.

29
Q

What’s a constructor?

A

It’s a special type of method that creates a new object

public class Chick {
 public Chick() {
 System.out.println("in constructor");
 }
}

There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there’s no return type.

The purpose of a constructor is to initialize fi elds, although you can put any code in there. Another way to initialize fields is to do so directly on the line on which they’re declared.

For most classes, you don’t have to code a constructor—the compiler will supply a “do nothing” default constructor for you.

30
Q

What is the order of the specifier methods?

A

static - Used for class methods.
abstract- Used when not providing a method body.
final - Used when a method is not allowed to be overridden by a subclass.
synchronized
native - Used when interacting with code written in another language such as C++.
strictfp - Used for making floating-point calculations portable.

public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {} // DOES NOT COMPILE - modifier is not a valid optional specifier
public void final walk6() {} // DOES NOT COMPILE - because the specifier is after the return type
final public void walk7() {}

31
Q

What is the order of the specifier methods?

A

static - Used for class methods.
abstract- Used when not providing a method body.
final - Used when a method is not allowed to be overridden by a subclass.
synchronized
native - Used when interacting with code written in another language such as C++.
strictfp - Used for making floating-point calculations portable.

public void walk1() {}
public final void walk2() {}
public static final void walk3() {}
public final static void walk4() {}
public modifier void walk5() {} // DOES NOT COMPILE - modifier is not a valid optional specifier
public void final walk6() {} // DOES NOT COMPILE - because the specifier is after the return type
final public void walk7() {}

*Java allows the optional specifi ers to appear before the access modifier

32
Q

What is the rule for naming variables and methods?

A

An identifier may only contain letters, numbers, $, or _.
Also, the first character is not allowed to be a number, and reserved words are not allowed.

public void walk1() { }
public void 2walk() { } // DOES NOT COMPILE
public walk3 void() { } // DOES NOT COMPILE
public void Walk_$() { }
public void() { } // DOES NOT COMPILE