Section 1 Flashcards

1
Q

Explain the difference between JDK, JRE, and JVM

A

JDK stands for Java Development Kit and is the development platform for developing Java applications. It includes the JRE, the interpreter (java), the compiler (javac), an archiver (jar), and a documentation generator (javadoc).

JRE stands for Java Runtime Environment. It is a standalone component used to run Java programs. It communicates between the Java program and the operating system.

JVM stands for Java Virtual Machine and is a specification that facilitates a run-time environment in which Java’s bytecode can be implemented.

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

What makes Java a platform-independent programming language?

A

Java is platform-independent because it uses a virtual machine. Java programs are compiled into bytecode. Bytecode is effectively platform-independent. The JVM converts this bytecode into bytecode for the machine’s operating system.

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

Would it be correct to say that Java is not 100% Object-oriented?

A

Yes, it would be correct to say that Java is not 100% object-oriented because it utilizes eight kinds of primitive data types:

boolean, byte, short, char, int, long, float, double

These data types are not objects, although wrapper classes can be used to create objects of each class.

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

What is a constructor in Java?

A

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. The constructor has the same name as the class. It calls a default constructor if there is no constructor available in the class. In such a case, the Java compiler provides a default constructor by default.

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

Can we mark constructors as final?

A

Declaring the constructor as final is not possible.

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

What is a class in Java?

A

In Java, classes are templates used for object creation, and to define certain object data types and their methods.

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

What is Java?

A

Java is a high-level, object-oriented, robust, secure programming language, platform-independent, high-performance, Multithreaded, and portable programming language. It was developed by James Gosling
in June 1991. It can also be known as the platform as it provides its own JRE and API.

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

List the features of Java Programming language

A

Object-Oriented: Java follows the object-oriented paradigm which allows us to maintain our code as the combination of different types of objects that incorporate both data and behavior.

Portable: Java supports read-once-write-anywhere approach. We can execute the Java program on every machine. Java program (.java) is converted to bytecode (.class) which can be easily run on every machine.

Platform Independent: It is different from other programming languages like C and C++ which needs a platform to be executed. Java comes with its platform on which its code is executed. Java doesn’t depend upon the operating system to be executed.

Secured: Java is secured because it doesn’t use explicit pointers. Java also provides the concept of ByteCode and Exception handling which makes it more secured.

Robust: Java is a strong programming language as it uses strong memory management. The concepts like Automatic garbage collection, Exception handling, etc. make it more robust.

Multithreaded: We can write Java programs that deal with many tasks at once by defining multiple threads. The main advantage of multi-threading is that it doesn’t occupy memory for each thread. It shares a common memory area. Threads are important for multi-media, Web applications, etc.

Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It means classes are loaded on demand. It also supports functions from its native languages, i.e., C and C++.

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

What do you understand by Java virtual machine?

A

The Java virtual machine is an abstract (virtual) computer defined by a specification. It is a part of the java runtime environment. It enables a computer to run Java programs that are compiled into Java bytecode.

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

How many types of memory areas are allocated by JVM?

A

Class(Method) Area: Class Area stores per-class structures such as the runtime constant pool, field, method data, and the code for methods.

Heap: It is the runtime data area in which the memory is allocated to the objects

Stack: Java Stack stores frames. It holds local variables and partial results, and plays a part in method invocation and return. Each thread has a private JVM stack, created at the same time as the thread. A new frame is created each time a method is invoked. A frame is destroyed when its method invocation completes.

Program Counter Register: PC (program counter) register contains the address of the Java virtual machine instruction currently being executed.

Native Method Stack: It contains all the native methods used in the application.

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

What is JIT compiler?

A

The Just-In-Time compiler is used to improve performance. It compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

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

What gives Java its ‘write once and run anywhere’ nature?

A

The bytecode. Java compiler converts the Java programs into the class file (Byte Code) which is the intermediate language between source code and machine code. This bytecode is not platform specific and can be executed on any computer.

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

What is classloader?

A

Classloader is a subsystem of JVM which is used to load class files. Whenever we run the java program, it is loaded first by the classloader. There are three built-in classloaders in Java.

Bootstrap ClassLoader: This is the first classloader which is the superclass of Extension classloader. It loads the rt.jar file which contains all class files of Java Standard Edition like java.lang package classes, java.net package classes, java.util package classes, java.io package classes, java.sql package classes, etc.

Extension ClassLoader: This is the child classloader of Bootstrap and parent classloader of System classloader. It loads the jar files located inside $JAVA_HOME/jre/lib/ext directory.

System/Application ClassLoader: This is the child classloader of Extension classloader. It loads the class files from the classpath. By default, the classpath is set to the current directory. You can change the classpath using “-cp” or “-classpath” switch. It is also known as Application classloader.

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

If no arguments are provided on the command line when running a program, what will the value stored in the String array passed into the main() method, empty or NULL?

A

Empty, not null

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

What is the default value of local variables?

A

Local variables are not initialized to any default value, neither primitives nor object references.

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

What are the various access specifiers/modifiers in Java?

A

In Java, access specifiers are the keywords which are used to define the access scope of the method, class, or a variable. In Java, there are four access specifiers given below.

Public can be accessed by any class or method.

Protected can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.

Default are accessible within the package only. By default, all the classes, methods, and variables are of default scope.

Private can be accessed within the class only.

17
Q

What is the purpose of static methods and variables?

A

Basically, static is used for a constant variable or a method that is the same for every instance of a class. It can be used with variables, methods, blocks and nested classes. It is a keyword that is used to share the same variable or method of a given class.

18
Q

What are Marker (or Tagging) Interfaces, and why don’t they have any methods?

A

A marker interface is an interface that doesn’t have any methods or constants inside it. It provides run-time type information about objects, so the compiler and JVM have additional information about the object.

Java has many built-in marker interfaces, such as Serializable, Cloneable, and Remote

Let’s take the example of the Cloneable interface. If we try to clone an object that doesn’t implement this interface, the JVM throws a CloneNotSupportedException. Thus, the Cloneable marker interface is an indicator to the JVM that we can call the Object.clone() method.

In the same way, when calling the ObjectOutputStream.writeObject() method, the JVM checks if the object implements the Serializable marker interface. When this isn’t the case, a NotSerializableException is thrown. Therefore, the object isn’t serialized to the output stream.

19
Q

What are the advantages of Packages in Java?

A

Packages avoid name clashes.

A package provides access control.

It is easier to locate the related classes.