Basics Flashcards

1
Q

class?

A
  • A class is a user-defined type that describes what a certain type of object will look like.
  • An object is an instance of a class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

public static void main() ?

A
  1. Public: It is an Access modifier, which specifies from where and who can access the method. Making the main() method public makes it globally available. It is made public so that JVM can invoke it from outside the class as it is not present in the current class.
  2. Static: It is a keyword which is when associated with a method, makes it a class related method. The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.
  3. Void: It is a keyword and used to specify that a method doesn’t return anything. As main() method doesn’t return anything, its return type is void. As soon as the main() method terminates, the java program terminates too. Hence, it doesn’t make any sense to return from main() method as JVM can’t do anything with the return value of it.
  4. main: It is the name of Java main method. It is the identifier that the JVM looks for as the starting point of the java program. It’s not a keyword.
  5. String[] args: It stores Java command line arguments and is an array of type java.lang.String class. Here, the name of the String array is args but it is not fixed and user can use any name in place of it.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. Why is Java not a pure object oriented language?
A

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
  1. Pointers are used in C/ C++. Why does Java not make use of pointers?
A

Pointers are quite complicated and unsafe to use by beginner programmers. Java focuses on code simplicity, and the usage of pointers can make it challenging. Pointer utilization can also cause potential errors. Moreover, security is also compromised if pointers are used because the users can directly access memory with the help of pointers.

Thus, a certain level of abstraction is furnished by not including pointers in Java. Moreover, the usage of pointers can make the procedure of garbage collection quite slow and erroneous. Java makes use of references as these cannot be manipulated, unlike pointers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. What do you understand by an instance variable and a local variable?
A

1.Instance variables: are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost.

All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.
2.Local variables: are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. What do you mean by data encapsulation?
A

Data Encapsulation is an Object-Oriented Programming concept of hiding the data attributes and their behaviors in a single unit.
It helps developers to follow modularity while developing software by ensuring that each object is independent of other objects by having its own methods, attributes, and functionalities.
It is used for the security of the private properties of an object and hence serves the purpose of data hiding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Tell us something about JIT compiler.
A
JIT stands for Just-In-Time and it is used for improving the performance during run time. It does the task of compiling parts of byte code having similar functionality at the same time thereby reducing the amount of compilation time for the code to run.
The compiler is nothing but a translator of source code to machine-executable code. But what is special about the JIT compiler? Let us see how it works:
First, the Java source code (.java) conversion to byte code (.class) occurs with the help of the javac compiler.
Then, the .class files are loaded at run time by JVM and with the help of an interpreter, these are converted to machine understandable code.
JIT compiler is a part of JVM. When the JIT compiler is enabled, the JVM analyzes the method calls in the .class files and compiles them to get more efficient and native code. It also ensures that the prioritized method calls are optimized.
Once the above step is done, the JVM executes the optimized code directly instead of interpreting the code again. This increases the performance and speed of the execution.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Features of java ?

A
  1. Object Oriented
  2. Platform Independent
  3. Robust
  4. Interpreted
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
  1. Object Oriented
A

In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
  1. Why is Java a platform independent language?
A

Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it.

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

Java makes an effort to eliminate error prone situations by emphasizing
mainly on compile time error checking and runtime checking.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
  1. Java is Interpreted ?
A

Java byte code is translated on the fly to native machine
instructions and is not stored anywhere. The development process is more rapid
and analytical since the linking is an incremental and light-weight process.

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

Example prgm to print hello in java?

A
public class MyFirstJavaProgram
 {
 public static void main(String []args)
 {
 System.out.println("Hello World");
 }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Save, compile, & run -java program ?

A

Save the file as: filename.java.
C:> javac MyFirstJavaProgram.java
C:> java MyFirstJavaProgram
Hello World

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

enums ?

A
  • Enums were introduced in Java 5.0. Enums restrict a variable to have one of only a few predefined values.
  • The values in this enumerated list are called enums.
    With the use of enums it is possible to reduce the number of bugs in your code.
  • For example, if we consider an application for a fresh juice shop, it would be possible to restrict the glass size to small, medium, and large. This would make sure that it would not allow anyone to order any size other than small, medium, or large.
    -
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Inheritance

A
In Java, classes can be derived from classes. Basically, if you need to create a new class
and here is already a class that has some of the code you require, then it is possible to
derive your new class from the already existing code.
This concept allows you to reuse the fields and methods of the existing class without having
to rewrite the code in a new class. In this scenario, the existing class is called the
superclass and the derived class is called the subclass.
17
Q

Interfaces

A

In Java language, an interface can be defined as a contract between objects on how to
communicate with each other. Interfaces play a vital role when it comes to the concept of
inheritance.
An interface defines the methods, a deriving class (subclass) should use. But the
implementation of the methods is totally up to the subclass.

18
Q

super keyword?

A

The super keyword in Java is a reference variable which is used to refer immediate parent class object.

19
Q

this keyword?

A

The this() constructor call can be used to invoke the current class constructor.

20
Q

instance variable? vs local variable?

A

instance variable:-

  • They are defined in class but outside the body of methods.
  • These variables are destroyed when the object is destroyed.
  • It can be accessed throughout the class.

local variable:-

  • They are defined as a type of variable declared within programming blocks or subroutines.
  • These variables are destroyed when the constructor or method is exited.
  • Its access is limited to the method in which it is declared.
  • scope = limited to a method an
  • its initialization is mandatory.
21
Q

Method overriding ?

A

Method overriding is the concept in which two methods having the same method signature are present in two different classes in which an inheritance relationship is present. A particular method implementation (already present in the base class) is possible for the derived class by using method overriding.

22
Q

Method overloading ?

A

In Java, method overloading is made possible by introducing different methods in the same class consisting of the same name. Still, all the functions differ in the number or type of parameters. It takes place inside a class and enhances program readability.

The only difference in the return type of the method does not promote method overloading. The following example will furnish you with a clear picture of it.

23
Q

final, finally and finalize keywords

A

(1) Final: If any restriction is required for classes, variables, or methods, the final keyword comes in handy. Inheritance of a final class and overriding of a final method is restricted by the use of the final keyword. The variable value becomes fixed after incorporating the final keyword. Example:

final int a=100;
a = 0; // error
The second statement will throw an error.

(2) Finally: It is the block present in a program where all the codes written inside it get executed irrespective of handling of exceptions. Example:

try {
int variable = 5;
}
catch (Exception exception) {
System.out.println(“Exception occurred”);
}
finally {
System.out.println(“Execution of finally block”);
}
(3) Finalize: Prior to the garbage collection of an object, the finalize method is called so that the clean-up activity is implemented. Example:

public static void main(String[] args) {
String example = new String("InterviewBit");
example = null;
System.gc(); // Garbage collector called
}
public void finalize() {
// Finalize called
}
24
Q

When can you use super keyword?

A
The super keyword is used to access hidden fields and overridden methods or attributes of the parent class.
Following are the cases when this keyword can be used:
Accessing data members of parent class when the member names of the class and its child subclasses are same.
To call the default and parameterized constructor of the parent class inside the child class.
Accessing the parent class methods when the child classes have overridden them.
25
Q

Can the static methods be overloaded?

A
Can the static methods be overloaded?
Yes! There can be two or more static methods in a class with the same name but differing input parameters.
26
Q
  1. Can the static methods be overridden?
A
Can the static methods be overridden?
No! Declaration of static methods having the same signature can be done in the subclass but run time polymorphism can not take place in such cases.
Overriding or dynamic polymorphism occurs during the runtime, but the static methods are loaded and looked up at the compile time statically. Hence, these methods cant be overridden.
27
Q

What is the main objective of garbage collection?

A

garbage collection- The main objective of this process is to free up the memory space occupied by the unnecessary and unreachable objects during the Java program execution by deleting those unreachable objects.

This ensures that the memory resource is used efficiently, but it provides no guarantee that there would be sufficient memory for the program execution.

28
Q

What part of memory - Stack or Heap

A

heap