ch1 Flashcards

(85 cards)

1
Q

What is an object?

A

It is an instance of a class (in memory)

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

What are the two primary elements of a class?

A

fields/variables
methods

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

What are variables?

A

hold the state of the program

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

what are methods?

A

Block of code that performs it’s duty when called

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

What are the three types of comments?

A

// single
/*
* multi
*/
/**
*javadoc
*/

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

Can you have two classes in the same file?

A

yes

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

Given the following,

public class Animal{

}

class Animal2{

}

What should the filename be named? Why?

A

Animal because the public class need to match the filename.
Hence the name of the file needs to be Animal.java and not Animal2.java

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

What the main method in java

A

public static void main(String[] args)
{
log.info(“Hello”);
}

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

How do you compile a hello.java?

A

javac hello.java
java hello.java (execute it)

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

What happens when the hello.java is compiled?

A

It converts to bytecode, i.e hello.class which is something that the JVM care read and exectue

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

Name the rules of a file name?

A

The name must begin with a letter or the symbol $ or _.
Subsequent characters may also be numbers.
You cannot use the same name as a Java reserved word. As you might imagine, a
reserved word is a keyword that Java has reserved so that you are not allowed to use it.
Remember that Java is case sensitive, so you can use versions of the keywords that only
differ in case. Please don’t, though

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

What package is automatically imported?

A

java.lang
contains System.

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

How do you create an instance of a class?

A

simply write new
Random r = new Random();

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

Make a constructor for the following class :
public class Hello{

}

A

public Hello(){
System.out.println(“Constructor”);
}

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

What is a constructor?

A

It is a method that matches the class name and does not have a return type.

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

Is this a contructor?
public void Hello(){}

A

no because it has a return type = void

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

What is the purpose of a constructor?

A

Constructors create Java objects.

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

Make an instance variable

A

public class hello{
int number = 0; //instance variable
}

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

What is a code block

A

The code between braces is called a code block

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

How to count an instance initializer?

A

code that are outside the method

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

What are the rules of order or initialization?

A

Fields and instance initializer blocks are run in the order in which they appear in the file

The constructor runs after all fields and instance intializers blocks have run

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

List the 8 primities data types

A

int
double
float
char
boolean
long
short
byte

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

how do you initialize a variable?

A

int q = 1;

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

Are the following declaration legal?

boolean b1, b3;
int i, int i=1;
double d1, double d2;
int i; int w;
int num; number;
int i, double number;

A

1 fine
2. No
NO for double. you need to do double d1, d2;
int i and int w fine
int num; number; no
last one no because it needs to be of the same data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why do local variable need to be initialized?
Cause they do not have default values.
26
What is a local variable?
A variable defined within a method. They need to be initialized. They do not have defaut values.
27
What is the default value of initialization of the following: boolean byte,short, int, long double, float All object references
false 0 0.0 null
28
State the scope of the local variable
from the declaration to the end of the block
29
State the instance variable
in scope from declaration until object garbage collected public class Hell{ int hello = 0; //instance variable }
30
Class variables
In scope from declaration until the program ends static int MAX_LENGHT = 0;
31
Order the element in a class
P - package I - import C - class declaration Methods and fields go anywhere
32
Where are all the objects stored in java
My program's memory called the heap
33
What is garbage collection?
The process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program
34
System.gc()
not guaranteed to run
35
Name the range of the octal.
0-7
36
Hexadecimal
0-9 x X A-F a-f
37
Binary
0-1 b B
38
Which takes precedence? import aquarium.*; import aquarium.Water;
Always the one with the class name take the precedence
39
Can you import methods?
no only classes
40
Can you do this? import aquarium.Water; import aquarium.jellies.Water;
You cannot cannot specify the same classname in two imports
41
What makes java secure?
it runs inside JVM. This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.
42
What makes Java simple?
Java was intended to be simpler than C++. In addition to eliminating pointers, it got rid of operator overloading. In C++, you could write a + b and have it mean almost anything
43
What makes java robust?
ne of the major advantages of Java over C++ is that it prevents memory leaks. Java manages memory on its own and does garbage collection automatically. Bad memory management in C++ is a big source of errors in programs.
44
Why java called platform independent language?
Java is an interpreted language because it gets compiled to bytecode. A key benefit is that Java code gets compiled once rather than needing to be recompiled for different operating systems.
45
Why is java OOP?
Java is an object-oriented language, which means all code is defi ned in classes and most of those classes can be instantiated into objects
46
When is finalize() run?
when the object is eligible for gc
47
Can finalize() be called twice?
Never. Might or might not be called once. but never twice.
48
Why do local variable need to be initialized?
Cause they do not have default values.
49
When does finalize() gets called?
If the garbage collector tries to collect the object. If the garbage collector doesnt run, the method doesnt get called
50
Can finalize get called twice?
NEVER
51
What's the output? ``` public class orderPractise { int birds=10; public static void main(String[] args) { int count =1, birds=5; System.out.println(count+birds); } } ```
6
52
What's the output? ``` public class orderPractise { int birds=10; public static void main(String[] args) { int count =1; System.out.println(count+birds); } } ```
it wont compile
53
What is the output? ``` public class orderPractise { private static int yesterday =1; int tomorrow = 10; public static void main(String[] args) { orderPractise tolls = new orderPractise(); int today =20, tomorrow=40; System.out.println(orderPractise.yesterday+ tolls.tomorrow+ today); } } ```
31
54
Can Java bytecode can often be easily decoded/decompiled.?
no
55
What are the property of JVM?
It supports platform independence. It manages memory for the application. It translates Java instructions to machine instructions
56
public class InputImports { public void read(Files files) { Paths.get("name"); } } Can you do this? import java.nio.*; import java.nio.file.Files; import java.nio.file.Paths;
No, wildcard should be at the end.
57
Given, import java.nio.file.Files; import java.nio.file.Paths; Can you do the following? import java.nio.*.*;
Only one wildcard is needed and must be at the end
58
Given, import java.nio.file.Files; import java.nio.file.Paths; Can you do the following? import java.nio.files.Paths.*?
You can only import class names not methods.
59
What is default package used for?
unnamed package and should be used for throwaway code.
60
The fully qualified name for String is?
java.lang.String
61
Can comments appear before a package? How about after a package?
Yes
62
Can a multiline comment contain special characters? ``` /* comments can exist 003423/12323##$%^^&y&y!** >.< ^^ */ ```
yes
63
What is the output? String name = "/*Harry */ Paul"; System.out.println(name);
/*Harry */ Paul
64
When making a class in java, what are the mandatory fields?
class keyword name of the class class body class hello{ }
65
Can the main method be written this way? static public void main(String[] args) {}
yes
66
JVM will run which method? ``` public class Practiceee { static public void main(String args) { System.out.println("hello"); } public static void main(int num) { System.out.println("this"); } static public final void main(String... args) { System.out.println("yo"); } } ```
yo
67
Why wont this compile? import java.util.Date; import java.sql.Date;
Does not compile because code to import with the same name from different package wont compile.
68
Can you do this? ``` import java.util.Date; public class Practiceee { static public final void main(String... args) { System.out.println("yo"); Date date1; java.sql.Date date3; } } ```
yes
69
Can access modifiers be applied to all types of java entities?
Only to classes, interfaces and their members( instance and class variables and methods) NOT Local variables and method parameters
70
True or false? The javac command compiles directly into native machine code.
false first bytecode then native machine code.
71
Is bytecode human readable?
No
72
Will it compile? ``` import java.util.*; import java.sql.*; public class Birthday { private Date rob = new Date(); java.sql.Date Nelly; } ```
No because it will complain about ambigious at private Date rob = new Date();
73
``` import java.util.*; import java.sql.*; public class Birthday { java.sql.Date Nelly = new java.sql.Date(10L); java.sql.Date Nelly2 = new java.sql.Date(10); java.sql.Date Nelly3 = new java.sql.Date(); } ```
Nelly, Nelly2 is fine Nelly3 is not because it requires some params
74
``` import java.util.*; import java.sql.*; public class Birthday { java.sql.Date Nelly = new java.sql.Date(10L); java.sql.Date Nelly2 = new java.sql.Date(10); java.sql.Date Ann = new Date(10); } ```
ann doesnt compile bc ambigious
75
Will this compile? ``` import java.util.*; public class Birthday { java.sql.Date Nelly = new java.sql.Date(10L); java.sql.Date Nelly2 = new java.sql.Date(10); java.util.Date Ann = new Date(); } ```
Yes
76
Will this compile? import java.util.*; public class Birthday { java.sql.Date Nelly = new java.sql.Date(10L); java.sql.Date Nelly2 = new java.sql.Date(10); Date Ann = new Date(); Date Anny = new Date(3); }
Yes
77
Will this compile? ``` import java.util.*; import java.sql.*; public class Birthday { private Date rob = new Date(); private java.util.Date sharon = new java.util.Date(); } ```
No ambigious
78
Will this compile? ``` import java.util.Date; import java.sql.*; public class Birthday { private Date rob = new Date(); private java.util.Date sharon = new java.util.Date(); } ```
Yes
79
Will it compile? ``` import java.util.*; import java.sql.Date; public class Birthday { private Date rob = new Date(); private java.util.Date sharon = new java.util.Date(); } ```
No becaue sqlDate need an int or long in the params
80
``` import java.util.*; import java.sql.*; public class Birthday { private java.util.Date rob = new Date(); private java.util.Date sharon = new java.util.Date(); } ```
No still ambigius. fix it by private java.util.Date rob = new java.util.Date();
81
What is the technique of structuring programming data as a unit consisting of attributes, with actions defined on the unit.
Object orientation
82
Java takes the name of the class as a parameter.
true javac Hello.java so Hello is the parameter
83
What is this called? public int numberVisitors(int month)
method signature
84
What is a method signature?
The full declaration of a method is called a method signature.
85
why does the main method have a void return type?
Bc it is considered a good practise to return void if the method changes the state of the object. .the main() method changes the program state from started to finished.