Chapter 1 : Basic Concepts Flashcards
Chapter 1
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)
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{}
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)); } }
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)
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.*;
Because explicit imports have precedence over ones with wildcards
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"); } } }
It is missing an import statement for ArrayList
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); }
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.
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); } }
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.
Name all Java primitives and the values they can hold
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
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
1,2,3
Underscores can only exist between digits (not at either extreme or next to a decimal point)
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)
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.
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;
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)
Does line 1 do the same thing as line 2?
1 int a, b, c = 0;
2 int a = 0, b = 0, c = 0;
No
Line 1 only initializes variable c.
3 rules for naming variables, methods, classes, and fields
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)
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
b)
x has not been initialized. Local variables must be initialized before they can be used.
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
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.
What are the default field values for the types :
boolean byte, short, int, long float, double char Any object
boolean = false byte, short, int, long = 0 float, double = 0.0 char = '\n0000' (NUL) Any object = null