Basic java Flashcards

(101 cards)

1
Q

Do you need to import Java.lang library in a Java program

A

No, it is available by default

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

Can you have the class name of a java program different from the name of the .java file it resides in?

A

Yes but not in case of public classes, The file name and public class name should match else you will get a compile time error saying “class <> is public, should be declared in a file named <>.java”

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

How to escape a character like double quote, backslash etc.?

A

By putting a backslash() in front of the character to be escaped.

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

Scanner class

A

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods

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

should an IO class like Scanner be closed

A

Yes, this should be done to avoid memory leakage and can be done by calling close() method on the scanner class object

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

variables - private; methods - public

A

variables in a class should be declared as private to restrict access to them and allow access only through get and set methods. Hence methods should be declared public.

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

Primitive Types

A

boolean(True/false), byte(one byte), char(2 bytes), short, int, long, float, double

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

Reference types

A

Anything which does not belong to the primitive types is of reference type. Reference types start with a capital letter

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

Does reference type store the actual object

A

no, they store a reference to an object not the actual value of the object

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

constructor features

A

have the exact same name as the class; do not have a return type; a class can have as many constructors as needed but they should differ in parameters and/or types; once a constructor is defined the compiler won’t provide a default one, all versions of the constructors needed will have to be defined by the user

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

approximation in double type

A

For financial calculations avoid using double variables as double approximates values. Use third party libraries of bigdecimal insterad

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

What is input stream?

A

An object from which we can read a sequence of bytes is called an input stream

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

What is an output steam?

A

An object to which we can write a sequence of bytes is called an output stream

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

JDK

A

Jave development kit - Includes JRE - Needed for development in Java

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

JRE

A

Java runtime environment - needed to run Java applications

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

Line Comments

A

//

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

Block comments

A

/* … */

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

Javadoc comments

A

/** … */

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

line comments in a block comment

A

valid and no compile time error

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

Package naming conventions

A

Should be all lower case
use reverse domain name to insure uniqueness for example pluralsight.com will use com.pluralsight
Add further qualifiers to ensure uniqueness within the company/group

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

allowed characters for variable names

A

letters, numbers, dollar sign and underscore

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

Convention for variables names

A

only letters and numbers are used conventionally. Camel case is followed for casing

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

Four integer types with sizes

A

Byte - 8 bits
Short - 16 bits
Int - 32 bits
Long - 64 bits

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

The result of divide operation on floating point operands and integer operands

A

example 13.0/5.0 will result in 2.6 whereas 13/5 will give 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
The result of divide operation on floating point operands and integer operands
example 13.0/5.0 will result in 2.6 whereas 13/5 will give 2
26
Operator precedence
postfix then prefix then multiplicative then additive.
27
How are operators of equal precedence evaluated
left to right
28
Implicit type conversion rule for mixed integer types in an equation
Converted to largest integer type in the equation
29
Implicit conversion types for mixed floating types in an equation
Converted to double as double is the largest floating point type
30
Implicit conversion types for mixed integer and floating types in an equation
Converted to the largest floating point type in the equation
31
Conditional assignment
result = condition?true-value:false-value
32
Variables' scope
Variables that are in scope when the block start remain in scope inside the block Variables declared within a block are out of scope outside the blocks
33
Conditional logical operators
the expression at the right side of the operator is evaluated only if it is needed to determine the final result
34
Encapsulation
The concept of hiding internal representation of an object
35
visibility with no access modifiers
Also called package private. Visible only within its own package. Usable on classes and members of a class
36
Private access modifier
Visible only within its own class. Not usable on top level classes but only members of a class
37
Naming convention for classes
Should use 'Pascal Case'. The name should be a descriptive noun without any acronyms
38
THIS keyword
THIS is an implicit reference to the current object
39
What does null represent
It represents an uncreated object
40
Mechanisms to establish initial states of an object
Field initializers constructors Initialization blocks
41
initial values of variables vs fields
Variables need to be explicitly initialized whereas a fields initial state is established as part of object construction(initialed to "zero" meaning int types get 0, boolean gets false, double gets 0.0, reference type gets null and so on)
42
What does explicit constructor do to default constructor
Once we create a user defines constructor, the default constructor is no longer available. User will have to create all the constructors needed for the class
43
Characteristics of initialization blocks
- Shared across all constructors - Executes as if the code was placed at the start of each constructor - declared by including a block of code in side brackets {} (no method name of qualifiers) - If there are multiple initialization blocks in a class, they are called in the order in which they appear from top to bottom
44
order of execution of, constructors, filed initializations and initialization blocks
1) Field initialization 2) Initialization Block 3) Constructor
45
Where should a chained constructor call appear in a calling constructor?
Chained constructor call should be the first line in a calling constructor
46
Can a child class be assigned to a reference type of its parent class
Yes, but the object so obtained will have access to methods only visible to the parent class.
47
What happens if you declare a variable in a child class with the same name as one in its parent class?
The variable in the parent class gets hidden by the one in child class
48
Which overridden method is called when the reference and corresponding object are of different types
The method belonging to the type of object and not the reference is called
49
What annotation can be used to make sure the signature of the overridden method in child class matches that of the parent class
@override (used only at compile time to make sure the signature of the overridden methods match, no significance at runtime)
50
Which class is the root of the Java class hierarchy
Object class
51
== operator compares references, true or false
True
52
Super treats an object as if it is an instance of the base class
True
53
Syntax for defining a final class
final Class or final Class
54
Should a class be defined as abstract if a method inside it is
Yes, the class should have the qualifier abstract if at least one of its method is abstract
55
What does the intern method of String class do
it returns a canonical representation of a string object
56
What's the mutability of wrapper classes
Wrapper classes are immutable
57
What is boxing?
taking a primitive type and wrapper it in a wrapper classes is called boxing. valueOf method is used generally for this
58
What is unboxing?
Extracting a primitive value from a wrapper class is called unboxing. xxxValue() method is generally used for this (xxx is int, char, byte etc)
59
Method to convert String to primitive type
parseXxx() where Xxx corresponds to the primitive type
60
Method to convert String to wrapper class
valueOf()
61
Strings are immutable, True or False
True(that why we use string builder)
62
What should be the order of exception types in the catch block
catch block should catch the more specific exception type first then followed by a more general type
63
Exceptions in java travel up the call stack, True or False?
True
64
What are the three ways of making sure that the throws clause of an overriding method is compatible with the throws clause of the overridden method
1) Exclude exceptions in the method signature 2) Have the same exception as the overridden method 3) Have a derived exception(e.g. throwing fileNotFoundException instead of IOException)
65
Where should a package declaration appear in a class file
before any type declarations
66
What are the 3 ways to avoid explicitly qualifying types(with full package names)
1) Types in current package do not need not be qualified 2) Types in java.lang package do not need to be qualified 3) Use type imports
67
Where's the documentation for jar manifest
http://bit.ly/jarmanifest
68
How is start-up class identified in a JAR file
Usually it is done through manifest
69
What is an interface
It defines a contract that a class can confirm to. It doesn't provide and implementation
70
A class can extend more than one class
False
71
A clas can implement more than one interface
True
72
Are streams bidirectional?
No, they are unidirectional
73
Two kinds of streams
Byte Stream - Interact as binary data | Text Stream - interact as unicode characters
74
Class for reading byte stream
InputStream
75
Class for reading text stream
Reader
76
InputStream and OutputStream are abstract class, true or false
True
77
how to associate a type to an ArrayList
ArrayList list = new ArrayList<>() | e.g. ArrayList list = new ArrayList<>()
78
Which collection doesn't implement collection interface
Map
79
Method to get an Array out of a collection
toArray
80
Method to get an Array as a collection
asList
81
For properties files, white spaces before and after = and : signs are ignored
True
82
What happens if no = or : sign is provided in a property value pair
the first whitespace will be used as a key/value separator
83
Command to set a classpath from command prompt
set CLASSPATH=
84
Why do you need class paths
Java needs to know where to look for classes to load for a program or application
85
What delimiters are used to separate multiple classpaths from each other
windows system - ; | Unix system - :
86
What is the order order in which classpath folders are searched if there are more than one specified
order of appearance
87
command to run a java program from command prompt along with classpath name
java -cp
88
How to load classes in jars with classpath option on command prompt
java -cp
89
Running a class from within a jar with -jar option
java -jar | This loads the classes only within the jar and is used to load classes in a very controlled manner
90
About persistence of system properties set by setProperties method
The setProperties method changes the set of system properties for the current running application. These changes are not persistent. That is, changing the system properties within an application will not affect future invocations of the Java interpreter for this or any other application. The runtime system re-initializes the system properties each time its starts up. If changes to system properties are to be persistent, then the application must write the values to some file before exiting and read them in again upon startup
91
What does the logger level SEVERE specify
Numeric value - 1000 | Description - Serious failure
92
What does the logger level WARNING specify
Numeric value - 900 | Description - Potential Problem
93
What does the logger level INFO specify
Numeric value - 800 | Description - General info
94
What does the logger level CONFIG specify
Numeric value - 700 | Description - Configuration info
95
What does the logger level FINE specify
Numeric value - 500 | Description - General developer info
96
What does the logger level FINER specify
Numeric value - 400 | Description - Detailed developer info
97
What does the logger level FINEST specify
Numeric value - 300 | Description - Specialized developer info
98
When is the LogManager object created and can it be changed subsequently
The LogManager object is created during class initialization and cannot be changed subsequently
99
What does the LogManager object does?
Maintains a hierarchical namespace of logging objects | Manages a set of logging control properties
100
There is a single global LogManager object that is used to maintain a set of shared state about Loggers and log services
True
101
How to retrieve a LogManager object
using LogManager.getLogManager()