Explain about Different operators in python?
Operators are symbols that perform operations on variables and values. For example, + is an operator used for addition, while * is also an operator used for multiplication.
Operators in Java can be classified into 5 types:
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations on variables and data. For example,
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Operation (Remainder after division)
Assignment Operators
Assignment operators are used in Java to assign values to variables.
= a = b; a = b;
+= a += b; a = a + b;
-= a -= b; a = a - b;
*= a *= b; a = a * b;
/= a /= b; a = a / b;
%= a %= b; a = a % b;
Relational Operators
Relational operators are used to check the relationship between two operands.
== Is Equal To 3 == 5 returns false
!= Not Equal To 3 != 5 returns true
> Greater Than 3 > 5 returns false
< Less Than 3 < 5 returns true
>= Greater Than or Equal To 3 >= 5 returns false
<= Less Than or Equal To 3 <= 5 returns true
Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in decision making.
&& (Logical AND) expression1 && expression2 true only if both expression1 and expression2 are true.
|| (Logical OR) expression1 || expression2 true if either expression1 or expression2 is true.
! (Logical NOT) !expression true if expression is false and vice versa.
Unary Operators
Unary operators are used with only one operand. For example, ++ is a unary operator that increases the value of a variable by 1. That is, ++5 will return 6.
+ Unary plus: not necessary to use since numbers are positive without using it
- Unary minus: inverts the sign of an expression
++ Increment operator: increments value by 1
-- Decrement operator: decrements value by 1
! Logical complement operator: inverts the value of a boolean
Bitwise Operators
Bitwise operators in Java are used to perform operations on individual bits.
~ Bitwise Complement
<< Left Shift
>> Right Shift
>>> Unsigned Right Shift
& Bitwise AND
^ Bitwise exclusive OR
Explain about various Control statements?
Control Statements are the base of any programming language. Using control statements we implement real world scenarios in programs. There are three types of Control Statements in Java:
Decision-Making Statements
Loop Statements
Jump or Branch Statements
Decision-Making statements
Decision-making statements in Java are similar to making a decision in real life, where we have a situation and based on certain conditions we decide what to do next.
If-else Statements
These are the simplest and yet most widely used control statements in Java. The if statement is used to decide whether a particular block of code will be executed or not based on a certain condition.
If the condition is true, then the code is executed otherwise not.
That's where else statement is used. In this, if the condition is true then the code inside the if block is executed otherwise the else block is executed.
Switch Statement
Switch statements are almost similar to the if-else-if ladder control statements in Java. It is a multi-branch statement. It is a bit easier than the if-else-if ladder and also more user-friendly and readable.
The switch statements have an expression and based on the output of the expression, one or more blocks of codes are executed.
Loop Statement
Java provides a set of looping statements that executes a block of code repeatedly while some condition evaluates to true. Looping control statements in Java are used to traverse a collection of elements, like arrays.
While loop
The while loop statement is the simplest kind of loop statement. It is used to iterate over a single statement or a block of statements until the specified boolean condition is false.
Do-while loop
The Java do-while loop statement works the same as the while loop statement with the only difference being that its boolean condition is evaluated post first execution of the body of the loop. Thus it is also called exit controlled looping statement.
For loop
Unlike the while loop control statements in Java, a for loop statement consists of the initialization of a variable, a condition, and an increment/decrement value, all in one line. It executes the body of the loop until the condition is false.
Jump/Branching Statement Break
The break statement as we can deduce from the name is used to break the current flow of the program. The break statement is commonly used in the following three situations:
Terminate a case block in a switch statement as we saw in the example of the switch statement in the above section.
To exit the loop explicitly, even if the loop condition is true.
Use as the alternative for the goto statement along with java labels, since java doesn’t have goto statements.
Continue Statement
This is similar to the break statement in the sense that it bypasses every line in the loop body after itself, but instead of exiting the loop, it goes to the next iteration.
Explain the life cycle of a thread?
In Java, a thread always exists in any one of the following states. These states are:
New
Whenever a new thread is created, it is always in the new state. For a thread in the new state, the code has not been run yet and thus has not begun its execution.
Active
When a thread invokes the start() method, it moves from the new state to the active state. The active state contains two states within it: one is runnable, and the other is running.
Runnable: A thread, that is ready to run is then moved to the runnable state. In the runnable state, the thread may be running or may be ready to run at any given instant of time. It is the duty of the thread scheduler to provide the thread time to run, i.e., moving the thread the running state.
When the thread gets the CPU, it moves from the runnable to the running state. Generally, the most common change in the state of a thread is from runnable to running and again back to runnable.
Blocked / Waiting
Whenever a thread is inactive for a span of time (not permanently) then, either the thread is in the blocked state or is in the waiting state.
If there are a lot of threads in the waiting or blocked state, then it is the duty of the thread scheduler to determine which thread to choose and which one to reject, and the chosen thread is then given the opportunity to run.
Timed Waiting
Sometimes, waiting for leads to starvation. For example, a thread (its name is A) has entered the critical section of a code and is not willing to leave that critical section. In such a scenario, another thread (its name is B) has to wait forever, which leads to starvation.
Terminated
A thread reaches the termination state because of the following reasons:
When a thread has finished its job, then it exists or terminates normally.
Abnormal termination: It occurs when some unusual events such as an unhandled exception or segmentation fault.
Write a short note on Inheritance?
Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also.
Terms used in Inheritance
Class: A class is a group of objects which have common properties. It is a template or blueprint from which objects are created.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
Explain the following
a)Polymorphism b)Packages
Polymorphism
The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form.
Real-life Illustration: Polymorphism
A person at the same time can have different characteristics. Like a man at the same time is a father, a husband, an employee. So the same person possesses different behavior in different situations. This is called polymorphism.
Polymorphism is considered one of the important features of Object-Oriented Programming.
In Java polymorphism is mainly divided into two types:
Compile-time Polymorphism
Runtime Polymorphism
Packages
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java can be categorized in two form, built-in package and user-defined package.
There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc.
Advantage of Java Package
1) Java package is used to categorize the classes and interfaces so that they can be easily maintained.
2) Java package provides access protection.
3) Java package removes naming collision.
Explain JDBC?
JDBC or Java Database Connectivity is a Java API to connect and execute the query with the database. It is a specification from Sun microsystems that provides a standard abstraction(API or Protocol) for java applications to communicate with various databases. It provides the language with java database connectivity standards. It is used to write programs required to access databases. JDBC, along with the database driver, can access databases and spreadsheets. The enterprise data stored in a relational database(RDB) can be accessed with the help of JDBC APIs.
Purpose of JDBC
Enterprise applications created using the JAVA EE technology need to interact with databases to store application-specific information. So, interacting with a database requires efficient database connectivity, which can be achieved by using the ODBC(Open database connectivity) driver. This driver is used with JDBC to interact or communicate with various kinds of databases such as Oracle, MS Access, Mysql, and SQL server database.
Types Of JDBC
Two-tier model: A java application communicates directly to the data source. The JDBC driver enables the communication between the application and the data source.
Three-tier model: In this, the user’s queries are sent to middle-tier services, from which the commands are again sent to the data source. The results are sent back to the middle tier, and from there to the user.
Explain The life cycle of applet?
In Java, an applet is a special type of program embedded in the web page to generate dynamic content. Applet is a class in Java.
The applet life cycle can be defined as the process of how the object is created, started, stopped, and destroyed during the entire execution of its application. It basically has five core methods namely init(), start(), stop(), paint() and destroy().These methods are invoked by the browser to execute.
Along with the browser, the applet also works on the client side, thus having less processing time.
nit(): The init() method is the first method to run that initializes the applet. It can be invoked only once at the time of initialization. The web browser creates the initialized objects, i.e., the web browser (after checking the security settings) runs the init() method within the applet.
start(): The start() method contains the actual code of the applet and starts the applet. It is invoked immediately after the init() method is invoked. Every time the browser is loaded or refreshed, the start() method is invoked. It is also invoked whenever the applet is maximized, restored, or moving from one tab to another in the browser. It is in an inactive state until the init() method is invoked.
stop(): The stop() method stops the execution of the applet. The stop () method is invoked whenever the applet is stopped, minimized, or moving from one tab to another in the browser, the stop() method is invoked. When we go back to that page, the start() method is invoked again.
destroy(): The destroy() method destroys the applet after its work is done. It is invoked when the applet window is closed or when the tab containing the webpage is closed. It removes the applet object from memory and is executed only once. We cannot start the applet once it is destroyed.
paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes like circle, square, trapezium, etc., in the applet. It is executed after the start() method and when the browser or applet windows are resized.
Explain about Classes and Objects? Object
An entity that has state and behavior is known as an object e.g., chair, bike, marker, pen, table, car, etc. It can be physical or logical (tangible and intangible). The example of an intangible object is the banking system.
State: represents the data (value) of an object.
Behavior: represents the behavior (functionality) of an object such as deposit, withdraw, etc.
Identity: An object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. However, it is used internally by the JVM to identify each object uniquely.
Classes
A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. It is a logical entity. It can't be physical.
A class in Java can contain:
Fields
Methods
Constructors
Blocks
Nested class and interface