Chapter 1- Java Building blocks Flashcards
What is happening on these lines of code?
1) int x = 10;
2) int x = new Integer(10);
3) String SomeClass obj = new SomeClass();
4) int a = 10, b = 20, c = 30;
5) String s1 = “123”, s2 = “hello”;
6 )int m = 20; int p = m = 10;
7) int a, b = 10, c = 20;
8) String s1 = “123”, s2;
9) int a = 10, int b;
10) int a, Object b;
11) int x = y = 10;
1) initialising x using an int literal 10
2) initialising x using a primitive wrapper class
3) initialising obj by creating a new instance of SomeClass
4) initialising each variable of the same type with a different value
5) same as no. 4
6) assign 20 to m for the first statement. For the second, evaluate from right to left, reset m to 10 and use the new value of m to initialise p; so m & p both equal 10
7) a is declared but not initialised. b and c are being declared as well as initialised
8) Only s1 is being initialised
9) Doesn’t compile as you can have only one type name in one statement.
10) same as no. 9
11) Doesn’t compile, y must be defined before using it to initialise x.
What will be printed?
public class TestClass {
static int i;
static float d;
static boolean f;
static String s;
public static void main(String[] args){
System.out.println(i);
System.out.println(d);
System.out.println(f);
System.out.println(s);
}
}
0 0.0 false null
Will this compile?
public class TestClass {
public static void main(String[] args){
int p;
System.out.println(p);
}
}
Compile error. local variables must be explictly initialised before use
Will this compile?
public class TestClass {
public static void main(String[] args) throws Exception {
int val;
val = 10;
System.out.println(val);
}
}
compiles fine and prints 10 Even though the variable val is not initialised when declared, it is assigned before its used.
Will this compile?
public class TestClass {
public static void main(String[] args) throws Exception {
int val;
int i = 0; //LINE 4
if(i == 0){ val = 10; }
System.out.println(val);
}
}
no it wont compile. Even though we know i == 0 is true, the compiler won’t know what the actual value of i will be at the time of execution because i is not a compile time constant. Therefore, the compiler concludes that if the if condition evaluates to false , the variable val will be left uninitialised. you can solve this making line 4 a final variable. this makes i a compile time constant meaning that i ==0 will always be true
Will this compile?
int val;
int i = 0;
if(i == 0) {
val = 10;
} else {
val = 20;
} System.out.println(val);
yes. Even though i isn’t a compile time constant(final) the compiler doesn’t have to know the value of i here. If-else is one statement and the compiler is now sure that no matter what the value of i is, val will definitely be assigned a value.
What is a final variable?
A variable whose value doesn’t change once its has a value assigned to it.
Will these lines of code compile when run independently?
class Data{
int x = 10;
}
public class TestClass {
public static void main(String[] args){
final Data d = new Data();
d = new Data(); // 1
d.x = 20; // 2
}
}
line 1 will not compile as d i final. // once assigned a value, you can’t reassign it another value / it can’t refer to another object. line 2 compiles as we aren’t changing d but using it to access an instance variable which we then assign the value of 10.
What are the rules for identifiers in java?
1) you can’t use a Java reserved word 2) The name must begin with either letters, the symbol $ or _ 3) subsequent characters can be numbers
Are these legal?
1) _also0K1d3ntifi3r
2) hollywood@vine
3) strictfp
4) goto*
5) native
6) __SStill0kbutKnotsonice$
- legal
- illegal @ isn’t recognised
- illegal, reserved word
- illegal, reserved word
- illegal, reserved word
- legal
What will be the output of compiling class B?
package one;
class A { // 1
protected int j = 12; // 2
}
package two
class B extends A { // 3
public static void main(String [] args) {
A a = new A(); // 4
}
}
Compilation will fail on lines 2 & 4.
class A has a defualt access modifier meaning it can’t be accessed from another package as a result, instantiating class A would also fail.
Given the code below, what is the output?
- public class Whiz {
- public static void main(String [] a) {
- int [] a = {1,2,3};
- System.out.println(a[3]);
- }
- }
Compilation will fail on line 3 as you can’t declare two variables with the same name in the same scope - the main method also has variable a
Which of the following is not a valid int literal?
A. 1_000
B. 012
C. 0B01
D. 0XE
E. None of these
E is correct.
As of java 7, integral types can be expressed using binary, octal & hexadecimal literals
B is octal literal
C is binary
D is hexadecimal
Which of the following is not a valid int literal?
A. 1_000
B. 012
C. 0B021
D. 0X33
E. 10
C is not a valid literal
this option uses 2 which is not possible in a binary number
Given:
……………. is responsible for the hardware- and operating system- independence of the Java SE platform, the small size of the compiled code(bytecodes) and platform security.
A. Java Development Kit (JDK)
B. Java Virtual Machine (JVM)
C. Java Runtime Environment (JRE)
D. Java SE platform
E. None of the above
B. Java Virtual Machine (JVM)
What is the role of the JDK?
The Java Development Kit is the superset of the JRE since it contains the JRE i.e JVM & libraries as well as dev tools such as java compile, debuggers and core classes necessary for developing applications
JDK is what you need to compile Java source code.
What does the JRE do?
Java Runtime Environment is a container that holds all the libraries, the JVM and other files needed to run java applications.
The JRE is the environment in which the JVM uses these libraries and files to run applications at runtime
JRE = JVM + Java Packages Classes (like util, math, lang, awt, swing etc) + runtime libraries.
What does the JVM do?
After our java program is compiled it is converted to byte code and a .class file is created. The (JVM) is responsible for converting this byte code to the machine-specific code.
provides platform independence & security
Which of the following must be included in a java source file to compile and generate bytecode?
A. Package declaration
B. Class declaration
C. Import statement/s
D. Main method
E. field declaration
Class declaration is required inorder to generate a .class file which contains the bytecode after compiling
The class doesnt have to have a main method for this .classfile to be generated, only the class signature.
What does the javac command do when run?
it compiles a .javafile and creates a .classfile that contains bytecode
How would you compile and run a class named Program.java?
To compile use - javac Program.java
To run use - java Program
What will be the output of the code below?
public class DataTypes {
static int x = 4;
public static void main (String [] args) {
for(int x = 5; x<10; x++)
x++;
System.out.println(x–);
}
}
the output will be 4.
Due to the absence of curly braces on the for statement, th print statement is not in scope for the loop thus it prints the static int varaible x.
Had brackets been around the print statement aswell, the answer would have been 678910.
for(int x = 5; x<10; x++) {
x++;
System.out.println(x–);
}
What is the output of this code?
public class Whiz {
public static void main (String [] args) {
int x =0;
do{
int y = x++;
System.out.println(x–);
} while(y<5);
}
}
Compilation fails as y was declared inside the do loop, it is out of scope when we try to access it out of that block
Which of the following are sufficient enough to run a Java program only?
A. Java Development Kit (JDK)
B. Java Virtual Machine (JVM)
C. Java Runtime Environment (JRE)
D. Java SE platform
E. None of the above
C. Java Runtime Environment (JRE)
JRE consists of the JVM and the core java classes and libraries