Sitemap
Manjiri kalkundrikar

Manjiri kalkundrikar

Hi, I'm Manjiri Kalkundrikar, with extensive knowledge in the software testing field.

Java

--

Q.1) What is JAVA?

Answer: Java is a high-level programming language and is platform-independent.

Java is a collection of objects. There are a lot of applications, websites, and games that are developed using Java.

Q #2) What are the features of JAVA?

Answer: Features of Java are as follows:

  • OOPS concepts

· Object-oriented

· Inheritance

· Encapsulation

· Polymorphism

· Abstraction

  • Platform independent: A single program works on different platforms without any modification.
  • High Performance: JIT (Just In Time compiler) enables high performance in Java. JIT converts the bytecode into machine language and then JVM starts the execution.
  • Multi-threaded: A flow of execution is known as a Thread. JVM creates a thread which is called the main thread. The user can create multiple threads by extending the thread class or by implementing the Runnable interface.

Name the Java IDE’s?

Answer: Eclipse and NetBeans are the IDE’s of JAVA.

Simple programs-

Java Program to Add Two Integers

Example: How to Print an Integer entered by an user

Press enter or click to view image in full size
Press enter or click to view image in full size

Example: How to Print an Integer entered by an user

import java.util.Scanner;

public class HelloWorld {

public static void main(String[] args) {

// Creates a reader instance which takes
// input from standard input — keyboard
Scanner reader = new Scanner(System.in);
System.out.print(“Enter a number: “);

// nextInt() reads the next integer from the keyboard
int number = reader.nextInt();

// println() prints the following line to the output screen
System.out.println(“You entered: “ + number);
}
}

O/P:

Enter a number: 10
You entered: 10

Steps to Implement Java Program

Implementation of a Java application program involves the following step. They include:

1) Creating the program

2) Compiling the program

3) Running the program

Example-

// This is a simple Java program.

// FileName : “HelloWorld.java”.

class HelloWorld {

// Your program begins with a call to main().

// Prints “Hello, World” to the terminal window.

public static void main(String args[])

{

System.out.println(“Hello, World”);

}

}

Class Definition

This line uses the keyword class to declare that a new class is being defined.

class HelloWorld {
    //    //Statements}

HelloWorld

It is an identifier that is the name of the class

Main Method

In the Java programming language, every application must contain a main method.

The main function(method) is the entry point of your Java application, and it’s mandatory in a Java program. whose signature in Java is:

public static void main(String[] args)

Explanation of the above syntax

· public: So that JVM can execute the method from anywhere.

· static: The main method is to be called without an object. The modifiers are public and static can be written in either order.

· void: The main method doesn’t return anything.

· main(): Name configured in the JVM. The main method must be inside the class definition. The compiler executes the codes starting always from the main function.

· String[]: The main method accepts a single argument, i.e., an array of elements of type String.

System.out.println("Hello, World");

This line outputs the string “Hello, World” followed by a new line on the screen. Output is accomplished by the built-in println( ) method. The System is a predefined class that provides access to the system and out is the variable of type output stream connected to the console.

Compiling the Program

javac HelloWorld.java

The compiler creates a HelloWorld.class (in the current working directory) that contains the bytecode version of the program. Now, to Execute our program, JVM(Java Virtual Machine) needs to be called using java, specifying the name of the class file on the command line, as shown:

java HelloWorld

Q. 3) What do you mean by Constructor?

The constructor name is always the same as the class name

Constructors are similar to the Method

A constructor can take arguments but never return value

Constructors are automatically called when we create object of class

Constructors are used for initialization

whatever task we want to perform at the start of class, we will place them in constructors.

//Constructor

public class A {

Public A()

{

System.out.println(“hello constructor”);

}

To call the constructor in other class

A o = new A();

E.g.

public class LoginPage {

WebDriver driver;

public LoginPage(WebDriver ldriver)

{

this.driver=ldriver;

}

To call the constructor in other class

LoginPage loginPage=PageFactory.initElements(driver, LoginPage.class);

loginPage.loginToCRM(excel.getStringData(“Login”, 0, 0), excel.getStringData(“Login”, 0, 1));

Q.4) What is meant by the Local variable and the Instance variable?

· Answer:

· Local variables are defined in the method and scope of the variables that exist inside the method itself.

· Instance variable is defined inside the class and outside the method and the scope of the variables exists throughout the class.

Q.5) What is a Class?

Answer: All Java codes are defined in a Class. It has variables and methods. a class is a basic building block., the code should be write inside the class boundary

Q.6) What is a Variable?

Variables — Variables are containers for storing data values, It is use to hold data ,variable value can be changed

there are different types of variables, for example: int,float,char,boolean…

int marks=80;

int percentage;

float calc=11.2f

boolean b=true;

char c =’a’;

Constants- It is use to hold data, it’s value can not change

final int marks=80;

Q.7) What is a Method?

Methods — A method is a block of code that only runs when it is called. It’s exact business logic has to be done. It contains a set of statements (or) instructions to satisfy the particular requirement.

Example:

Get Manjiri kalkundrikar’s stories in your inbox

Join Medium for free to get updates from this writer.

public class Addition{ //Class name declaration

int a = 5; //Variable declaration

int b= 5;

public void add(){ //Method declaration

int c = a+b;

}

}

Q. 8) What is an Object?

Answer: An instance of a class is called an object. Whatever method you want to call, check which class it belongs to create an object of a class and call the method

A o=new A();

o.testing();

Example:

public class Addition{

public static void main(String[] args){

Addition add = new Addition();//Object creation

}

}

The above code creates the object for the Addition class.

Q.9) What are the OOP concepts?

Answer: OOPs concepts include:

  • Inheritance
  • Encapsulation
  • Polymorphism
  • Abstraction
  • Interface

Q. 10) What is Inheritance?

Answer: Inheritance means one class can extend to another class. So that the codes can be reused from one class to another class. The existing class is known as the Super class whereas the derived class is known as a sub class.

Example:

Super class:

public class Manupulation(){

}

Sub class:

public class Addition extends Manipulation(){

}

Inheritance is only applicable to the public and protected members only. Private members can’t be inherited.

Q. 11) public

public is a Java keyword which declares a member’s access as public. Public members are visible to all other classes. This means that any other class can access a public field or method

Q.12) private

The private keyword is an access modifier used for attributes, methods and constructors, making them only accessible within the declared class.

Q. 13) What is Encapsulation

Encapsulation in Java refers to integrating data (variables) and code (methods) into a single unit.

Purpose of Encapsulation:

  • Protects the code from others.
  • Code maintainability.

Q.14) What is Polymorphism?

Answer: Polymorphism means many forms. Means same name and different use.

A single object can refer to the super-class or sub-class depending on the reference type which is called polymorphism

Polymorphism is applicable for overriding and not for overloading

Q.15) What is meant by Method Overriding?

method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes

Answer: Method overriding happens if the sub-class method satisfies the below conditions with the Super-class method:

  • Method name should be the same
  • The argument should be the same
  • Return type should also be the same

The key benefit of overriding is that the Sub-class can provide some specific information about that sub-class type than the super-class.

Q.16) What is meant by Overloading?

Method overloading in Java means having two or more methods (or functions) in a class with the same name and different arguments (or parameters). It can be with a different number of arguments or different data types of arguments

Answer: Method overloading happens for different classes or within the same class.

For method overloading, sub-class method should satisfy the below conditions with the Super-class method (or) methods in the same class itself:

  • Same method name
  • Different argument types
  • There may be different return types

Q.17) What is meant by Interface?

is defined as an abstract type used to specify the behavior of a class, It is a collection of abstract methods

Q.18) What is meant by Abstract class?

We can create the Abstract class by using the “Abstract” keyword before the class name. An abstract class can have both “Abstract” methods and “Non-abstract” methods that are a concrete class.

Q.19) static Keyword in Java

The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. Static variable is like a global variable and is available to all methods.

Q.20) Non-static Keyword in Java

A non-static method in Java can access static methods and variables as follows: A non-static method can access any static method without creating an instance of the class. Non static variable is like a local variable and they can be accessed through only instance of a class.

Static variable

Non static variable

Static variables can be accessed using class name

Non static variables can be accessed using instance of a class

Static variables can be accessed by static and non static methods

Non static variables cannot be accessed inside a static method.

Static variables reduce the amount of memory used by a program.

Non static variables do not reduce the amount of memory used by a program

Static variables are shared among all instances of a class.

Non static variables are specific to that instance of a class.

Static variable is like a global variable and is available to all methods.

Non static variable is like a local variable and they can be accessed through only instance of a class.

Q.21) ‘Final’ Keyword

The final keyword is a non-access modifier used for classes, attributes and methods, which makes them non-changeable (impossible to inherit or override).

Q.22) ‘This’ keyword

This keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Q.23) Exception

An exception (or exceptional event) is a problem that arises during the execution of a program

We have 2 types of error

- Compile time- Compile time error is syntactical error, grammar mistake in keyword, word in program

E.g. system.out.println(“Hi”);

In this sentence first ‘s’ should be capital

- Runtime- Run time error is called exception or logical error

Types of Exception

- Checked — exception which are find out by complier and complier displaying that exception could occur that kind of exception are called checked exception

- Unchecked- Only at a run time we get this exception

Q.24) Exception handling

Actions we want to perform in case of exception occurs at run time, we can exception handling

with Throws keyword- with help of this we can declare an exception and need to define exception classes

can throw only checked exceptions

Declare at method level

can use multiple classes separated by comma

With help of throws keywords we are not doing expecition handling ,It is just passing expection to somebody else and somebody else will handle that ,

Exception handling will be done by the try and catch

try-Catch

Used for exception handling

code will not break in case of exception

If we get some exception code will jump to catch block, catch is used for exception handling

Throw keyword

to manually throw an exception

Need to place in body of the method

Only 1 exception can throw at a time

Difference between Throws and Throw

Press enter or click to view image in full size

--

--

Manjiri kalkundrikar

Written by Manjiri kalkundrikar

Hi, I'm Manjiri Kalkundrikar, with extensive knowledge in the software testing field.