Basic java Flashcards

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

Operator precedence

A

postfix then prefix then multiplicative then additive.

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

How are operators of equal precedence evaluated

A

left to right

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

Implicit type conversion rule for mixed integer types in an equation

A

Converted to largest integer type in the equation

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

Implicit conversion types for mixed floating types in an equation

A

Converted to double as double is the largest floating point type

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

Implicit conversion types for mixed integer and floating types in an equation

A

Converted to the largest floating point type in the equation

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

Conditional assignment

A

result = condition?true-value:false-value

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

Variables’ scope

A

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

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

Conditional logical operators

A

the expression at the right side of the operator is evaluated only if it is needed to determine the final result

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

Encapsulation

A

The concept of hiding internal representation of an object

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

visibility with no access modifiers

A

Also called package private. Visible only within its own package. Usable on classes and members of a class

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

Private access modifier

A

Visible only within its own class. Not usable on top level classes but only members of a class

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

Naming convention for classes

A

Should use ‘Pascal Case’. The name should be a descriptive noun without any acronyms

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

THIS keyword

A

THIS is an implicit reference to the current object

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

What does null represent

A

It represents an uncreated object

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

Mechanisms to establish initial states of an object

A

Field initializers
constructors
Initialization blocks

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

initial values of variables vs fields

A

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
Q

What does explicit constructor do to default constructor

A

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
Q

Characteristics of initialization blocks

A
  • 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
Q

order of execution of, constructors, filed initializations and initialization blocks

A

1) Field initialization 2) Initialization Block 3) Constructor

45
Q

Where should a chained constructor call appear in a calling constructor?

A

Chained constructor call should be the first line in a calling constructor

46
Q

Can a child class be assigned to a reference type of its parent class

A

Yes, but the object so obtained will have access to methods only visible to the parent class.

47
Q

What happens if you declare a variable in a child class with the same name as one in its parent class?

A

The variable in the parent class gets hidden by the one in child class

48
Q

Which overridden method is called when the reference and corresponding object are of different types

A

The method belonging to the type of object and not the reference is called

49
Q

What annotation can be used to make sure the signature of the overridden method in child class matches that of the parent class

A

@override (used only at compile time to make sure the signature of the overridden methods match, no significance at runtime)

50
Q

Which class is the root of the Java class hierarchy

A

Object class

51
Q

== operator compares references, true or false

A

True

52
Q

Super treats an object as if it is an instance of the base class

A

True

53
Q

Syntax for defining a final class

A

final Class
or
final Class

54
Q

Should a class be defined as abstract if a method inside it is

A

Yes, the class should have the qualifier abstract if at least one of its method is abstract

55
Q

What does the intern method of String class do

A

it returns a canonical representation of a string object

56
Q

What’s the mutability of wrapper classes

A

Wrapper classes are immutable

57
Q

What is boxing?

A

taking a primitive type and wrapper it in a wrapper classes is called boxing. valueOf method is used generally for this

58
Q

What is unboxing?

A

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
Q

Method to convert String to primitive type

A

parseXxx() where Xxx corresponds to the primitive type

60
Q

Method to convert String to wrapper class

A

valueOf()

61
Q

Strings are immutable, True or False

A

True(that why we use string builder)

62
Q

What should be the order of exception types in the catch block

A

catch block should catch the more specific exception type first then followed by a more general type

63
Q

Exceptions in java travel up the call stack, True or False?

A

True

64
Q

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

A

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
Q

Where should a package declaration appear in a class file

A

before any type declarations

66
Q

What are the 3 ways to avoid explicitly qualifying types(with full package names)

A

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
Q

Where’s the documentation for jar manifest

A

http://bit.ly/jarmanifest

68
Q

How is start-up class identified in a JAR file

A

Usually it is done through manifest

69
Q

What is an interface

A

It defines a contract that a class can confirm to. It doesn’t provide and implementation

70
Q

A class can extend more than one class

A

False

71
Q

A clas can implement more than one interface

A

True

72
Q

Are streams bidirectional?

A

No, they are unidirectional

73
Q

Two kinds of streams

A

Byte Stream - Interact as binary data

Text Stream - interact as unicode characters

74
Q

Class for reading byte stream

A

InputStream

75
Q

Class for reading text stream

A

Reader

76
Q

InputStream and OutputStream are abstract class, true or false

A

True

77
Q

how to associate a type to an ArrayList

A

ArrayList list = new ArrayList<>()

e.g. ArrayList list = new ArrayList<>()

78
Q

Which collection doesn’t implement collection interface

A

Map

79
Q

Method to get an Array out of a collection

A

toArray

80
Q

Method to get an Array as a collection

A

asList

81
Q

For properties files, white spaces before and after = and : signs are ignored

A

True

82
Q

What happens if no = or : sign is provided in a property value pair

A

the first whitespace will be used as a key/value separator

83
Q

Command to set a classpath from command prompt

A

set CLASSPATH=

84
Q

Why do you need class paths

A

Java needs to know where to look for classes to load for a program or application

85
Q

What delimiters are used to separate multiple classpaths from each other

A

windows system - ;

Unix system - :

86
Q

What is the order order in which classpath folders are searched if there are more than one specified

A

order of appearance

87
Q

command to run a java program from command prompt along with classpath name

A

java -cp

88
Q

How to load classes in jars with classpath option on command prompt

A

java -cp

89
Q

Running a class from within a jar with -jar option

A

java -jar

This loads the classes only within the jar and is used to load classes in a very controlled manner

90
Q

About persistence of system properties set by setProperties method

A

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
Q

What does the logger level SEVERE specify

A

Numeric value - 1000

Description - Serious failure

92
Q

What does the logger level WARNING specify

A

Numeric value - 900

Description - Potential Problem

93
Q

What does the logger level INFO specify

A

Numeric value - 800

Description - General info

94
Q

What does the logger level CONFIG specify

A

Numeric value - 700

Description - Configuration info

95
Q

What does the logger level FINE specify

A

Numeric value - 500

Description - General developer info

96
Q

What does the logger level FINER specify

A

Numeric value - 400

Description - Detailed developer info

97
Q

What does the logger level FINEST specify

A

Numeric value - 300

Description - Specialized developer info

98
Q

When is the LogManager object created and can it be changed subsequently

A

The LogManager object is created during class initialization and cannot be changed subsequently

99
Q

What does the LogManager object does?

A

Maintains a hierarchical namespace of logging objects

Manages a set of logging control properties

100
Q

There is a single global LogManager object that is used to maintain a set of shared state about Loggers and log services

A

True

101
Q

How to retrieve a LogManager object

A

using LogManager.getLogManager()