ch1 Flashcards

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 (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
Q

Why do local variable need to be initialized?

A

Cause they do not have default values.

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

What is a local variable?

A

A variable defined within a method. They need to be initialized. They do not have defaut values.

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

What is the default value of initialization of the following:

boolean
byte,short, int, long
double, float
All object references

A

false
0
0.0
null

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

State the scope of the local variable

A

from the declaration to the end of the block

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

State the instance variable

A

in scope from declaration until object garbage collected

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

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

Class variables

A

In scope from declaration until the program ends

static int MAX_LENGHT = 0;

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

Order the element in a class

A

P - package
I - import
C - class declaration
Methods and fields go anywhere

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

Where are all the objects stored in java

A

My program’s memory called the heap

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

What is garbage collection?

A

The process of automatically freeing memory on the heap by deleting objects that are no longer reachable in your program

34
Q

System.gc()

A

not guaranteed to run

35
Q

Name the range of the octal.

A

0-7

36
Q

Hexadecimal

A

0-9 x X A-F a-f

37
Q

Binary

A

0-1 b B

38
Q

Which takes precedence?

import aquarium.*;
import aquarium.Water;

A

Always the one with the class name take the precedence

39
Q

Can you import methods?

A

no only classes

40
Q

Can you do this?
import aquarium.Water;
import aquarium.jellies.Water;

A

You cannot cannot specify the same classname in two imports

41
Q

What makes java secure?

A

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
Q

What makes Java simple?

A

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
Q

What makes java robust?

A

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
Q

Why java called platform independent language?

A

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
Q

Why is java OOP?

A

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
Q

When is finalize() run?

A

when the object is eligible for gc

47
Q

Can finalize() be called twice?

A

Never. Might or might not be called once. but never twice.

48
Q

Why do local variable need to be initialized?

A

Cause they do not have default values.

49
Q

When does finalize() gets called?

A

If the garbage collector tries to collect the object.
If the garbage collector doesnt run, the method doesnt get called

50
Q

Can finalize get called twice?

A

NEVER

51
Q

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);
	}
}
A

6

52
Q

What’s the output?

public class orderPractise {
int birds=10;
	public static void main(String[] args) {
		 int count =1;
		 System.out.println(count+birds);
	}
}
A

it wont compile

53
Q

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);
	}
}
A

31

54
Q

Can Java bytecode can often be easily decoded/decompiled.?

A

no

55
Q

What are the property of JVM?

A

It supports platform independence.
It manages memory for the application.
It translates Java instructions to machine instructions

56
Q

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;

A

No, wildcard should be at the end.

57
Q

Given,
import java.nio.file.Files;
import java.nio.file.Paths;

Can you do the following?
import java.nio..;

A

Only one wildcard is needed and must be at the end

58
Q

Given,
import java.nio.file.Files;
import java.nio.file.Paths;

Can you do the following?
import java.nio.files.Paths.*?

A

You can only import class names not methods.

59
Q

What is default package used for?

A

unnamed package and should be used for throwaway code.

60
Q

The fully qualified name for String is?

A

java.lang.String

61
Q

Can comments appear before a package?
How about after a package?

A

Yes

62
Q

Can a multiline comment contain special characters?
~~~

/*
comments can exist
003423/12323##$%^^&y&y!**
>.< ^^

*/

~~~

A

yes

63
Q

What is the output?

String name = “/*Harry */ Paul”;
System.out.println(name);

A

/*Harry */ Paul

64
Q

When making a class in java, what are the mandatory fields?

A

class keyword
name of the class
class body

class hello{

}

65
Q

Can the main method be written this way?
static public void main(String[] args) {}

A

yes

66
Q

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");
	}
}
A

yo

67
Q

Why wont this compile?

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

A

Does not compile because code to import with the same name from different package wont compile.

68
Q

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;
	}
}
A

yes

69
Q

Can access modifiers be applied to all types of java entities?

A

Only to classes, interfaces and their members( instance and class variables and methods)

NOT Local variables and method parameters

70
Q

True or false?
The javac command compiles directly into native machine code.

A

false
first bytecode then native machine code.

71
Q

Is bytecode human readable?

A

No

72
Q

Will it compile?

import java.util.*;
import java.sql.*;
public class Birthday {
	private Date rob = new Date();
	java.sql.Date Nelly;
	
}
A

No because it will complain about ambigious at private Date rob = new Date();

73
Q
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();
}
A

Nelly, Nelly2 is fine
Nelly3 is not because it requires some params

74
Q
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);
}
A

ann doesnt compile bc ambigious

75
Q

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();
}
A

Yes

76
Q

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);

}

A

Yes

77
Q

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();

}
A

No ambigious

78
Q

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();

}
A

Yes

79
Q

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();

}
A

No
becaue sqlDate need an int or long in the params

80
Q
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();

}
A

No still ambigius.
fix it by
private java.util.Date rob = new java.util.Date();

81
Q

What is the technique of structuring programming data as a unit consisting of
attributes, with actions defined on the unit.

A

Object orientation

82
Q

Java takes the name of the class as a parameter.

A

true

javac Hello.java
so Hello is the parameter