0% found this document useful (0 votes)
48 views13 pages

Core Java Unit-I

The document discusses Java programming concepts like what Java is, operators, JDK, variables, data types, and features of Java. It provides definitions and examples of these concepts. It also discusses the structure of a Java program and the main method.

Uploaded by

Junaid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views13 pages

Core Java Unit-I

The document discusses Java programming concepts like what Java is, operators, JDK, variables, data types, and features of Java. It provides definitions and examples of these concepts. It also discusses the structure of a Java program and the main method.

Uploaded by

Junaid Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CORE JAVA

UNIT-I
 2 marks
1. What is JAVA?
Ans. (1) Java is a programming language and a platform. Java is a high-level, robust,
object-oriented, and secure programming language.
(2) Java was developed by Sun Microsystems in the year 1995.
(3) James Gosling is known as the father of Java. Before Java, its name was Oak. Since
Oak was already a registered company, so James Gosling and his team changed the Oak
name to Java.
2. Define Operator.
Ans. (1) Operators are special characters used to instruct the Java compiler to perform an
operation on one, two, or three operands, and then return a result.
(2) Java operators take one or more arguments or operands and produce a value.
3. Define JDK.
Ans. (1) JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is a
software development environment which is used to develop Java applications and
applets.
(2) It physically exists. It contains JRE + development tools.
4. Define variable.
Ans. (1) A variable is a container which holds the value while the Java program is executed. A
variable is assigned with a data type.
(2) A variable is the name of memory location. It is a combination of "vary + able" that
means its value can be changed.
(3)There are three types of variables in java:
(i) local
(ii)instance
(iii)static.
5. What is conditional operator?
Ans. (1) In java, conditional operators check the condition and decides the desired result on
the basis of both conditions.
(2)There are conditional operators used for comparison for testing equality,
conditional-AND (&&) and conditional-OR (||) operators are used to form compound
conditions by grouping conditions together.
6. What is the purpose of system.out.println()?
Ans. (1) System.out.println() is a method in Java that prints a message to the standard
output and appends a newline character.
(2) It is widely use to display messages, data, or string on the screen as the output.
(3) It displays the arguments that are passed to it.
7. Define datatype.
Ans. (1) Data type defines a set of permitted values on which the operations can be
performed.
(2) A data type is of different sizes and values which is stored in variable.
(3) There are two types of data type in Java: -
(i) Primitive datatype- It includes Boolean, char, int, short, long, double, float.
(ii)Non-primitive datatype- It includes classes, interfaces, and arrays.
8. Write any two features in Java.
Ans. (1) Simple:
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to
Sun, Java language is a simple programming language because:
Java syntax is based on C++ (so easier for programmers to learn it after C++).
(2) Object-oriented:
Java is called an object-oriented programming language because it is based on the concepts
of objects and classes. Without the creation of object, it is impossible to write any code in
java.

 3 marks
1. Why Java is called Platform independent?
Ans. (1) Java is platform-independent because it is different from other languages like C,
C++, etc. which are compiled into platform-based machines.
(2) Java programs are interpreted to the native’s instruction set at run time.
(3) Java can be run on any operating system with any processor as long as the Java
interpreter is available on that system.
(4) Java executes under the control of a JVM, Java programs can run on any operating
system that provides a JVM.
2. Explain the purpose of switch – case statement.
Ans. (1) The switch statement or switch case in java is a multi-way branch statement.
It is useful for selecting some action from a number of alternatives.
(2) Th switch case in Java works like an if-else ladder ,i.e., multiple conditions can be
checked at once.
(3) The value inside the test expression must be byte, char, short or int. It cannot be a
long, double, float, string, or any other kind of object.
(4) The value inside the test expressions is compared against the case labels, which
are constant expressions.
3. List standard default values for built-in data types.
4. Explain Nesting if else statement with syntax?
Ans. The nested if statement represents the if block within another if block. Here, the inner
if block condition executes only when outer if block condition is true.
Syntax:
if(condition){
//code to be executed
if(condition){
//code to be executed
}
}
5. List three major differences between Java & C ++
Ans.

Comparison Index C++ Java


Platform- C++ is a platform- Java is platform-
independent dependent. independent.
Mainly used C++ is mainly used for Java is mainly used for
for system programming. application
programming.
Operator C++ supports operator Java doesn't support
Overloading overloading. operator
overloading.

6. Explain conditional operator with the help of example.


Ans. In java, conditional operators check the condition and decides the desired result on the
basis of both conditions.
For example:
Public class ConditionalOperatorExample
{
Public static void main(Sting args[])
{
Int x=5, y=4, z=7;
System.out.println(x>y && x>z || y<z);
System.out.println(x<z || y>z) && x<y);
}
}
Output:
True
False
7. Explain the if-else statement with the help of syntax and example?
Ans. The Java if-else statement tests the condition. It executes the if block if condition is true
otherwise else block is executed.
Syntax:
if(condition)
{
//code if condition is true
}
else
{
//code if condition is false
}
Example:
public class LeapYearExample {
public static void main(String[] args) {
int year=2020;
if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){
System.out.println("LEAP YEAR");
}
else{
System.out.println("COMMON YEAR");
}
}
}
Output:
LEAP YEAR

 5 marks
1. Explain the structure of Java program ?

Ans. A structure of a Java program contains the following elements:

(1) Documentation Section:


The documentation section is an important section but optional for a Java program. It
includes basic information about a Java program. To write the statements in the
documentation section, we use comments. The comments may be single-line, multi-
line, and documentation comments.
(2) Package Declaration:
The package declaration is optional. It is placed just after the documentation section.
In this section, we declare the package name in which the class is placed. We use the
keyword package to declare the package name.
(3) Import Statements
If we want to use any class of a particular package, we need to import that class. The
import statement represents the class stored in the other package. We use
the import keyword to import the class.
(4) Interface Section:
We can create an interface in this section if required. We use the interface keyword to
create an interface. It contains only constants and method declarations.
(5) Class Definition
Class Definition contains information about user-defined methods, variables, and
constants. Every Java program has at least one class that contains the main() method.
We use the class keyword to define the class. 
(6) Class Variables and Constants
In a Java program, the variables and constants are defined just after the class
definition. The variables and constants store values of the parameters. It is used
during the execution of the program.
(7) Main Method Class
 It is essential for all Java programs. Because the execution of all Java programs starts
from the main() method. In other words, it is an entry point of the class.
(8) Methods and Behaviour
In this section, we define the functionality of the program by using the methods. The
methods are the set of instructions that we want to perform. These instructions
execute at runtime and perform the specified task. 
2. Explain main() method with Syntax.
Ans.
(1) The main() is the starting point for JVM to start execution of a Java program.
Without the main() method, JVM will not execute the program.
(2) The syntax of the main() method is:
Public static void main(String args[])
(3) Public: It is an access specifier. We should use a public keyword before the
main() method so that JVM can identify the execution point of the program.
(4) Static: Static methods are the method which invokes without creating the
objects, so we do not need any object to call the main() method. we can
make a method static by using the keyword static.
(5) Void: In Java, every method has the return type. Void keyword
acknowledges the compiler that main() method does not return any value.
(6) Main(): It is a default signature which is predefined in the JVM. It is called by
JVM to execute a program line by line and end the execution after
completion of this method.
(7) String args[]: The main() method also accepts some data from the user. It
accepts a group of strings, which is called a string array.
3. Describe primitive data types in Java.
Lnhjkguyffhytiyhki87yu
4. Explain any five features of Java.

Ans. A list of most important features of Java language is given below:

(1) Simple:
Java is very easy to learn, and its syntax is simple, clean and easy to understand. According to
Sun, Java language is a simple programming language because:
Java syntax is based on C++ (so easier for programmers to learn it after C++).
(2) Object-oriented:
Java is called an object-oriented programming language because it is based on the concepts
of objects and classes. Without the creation of java, it is impossible to write any code in java.
(3) Platform Independent:
Java can be run on any operating system with any processor as long as the Java interpreter is
available on that system.
(4) Robust:
Robust simply means strong. Java is robust because it uses strong memory management.
There are exception-handling and the type checking mechanism in Java. All these points
make Java robust.
(5) Multi-threaded:
A thread is like a separate program, executing concurrently. We can write Java programs that
deal with many tasks at once by defining multiple threads.
5. Explain switch- case statement with example.
Ans. (1) The switch statement or switch case in java is a multi-way branch statement.
It is useful for selecting some action from a number of alternatives.
(2) Th switch case in Java works like an if-else ladder ,i.e., multiple conditions can be
checked at once.
Example of switch-case statement:

// Java Program to check the size


// using the switch...case statement

class Main {
public static void main(String[] args) {

int number = 44;


String size;

// switch statement to check size


switch (number) {

case 29:
size = "Small";
break;

case 42:
size = "Medium";
break;

// match the value of week


case 44:
size = "Large";
break;

case 48:
size = "Extra Large";
break;

default:
size = "Unknown";
break;

}
System.out.println("Size: " + size);
}
}

Output:

Size: Large

6. Explain any five mathematical functions in Java.

Ans: (1) Java Math class provides several methods to work on math calculations like
min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.

(2) If the size is int or long and the results overflow the range of value, the methods
addExact(), subtractExact(), multiplyExact(), and toIntExact() throw an ArithmeticException.

Following are the five mathematical functions in java:

(i) min(): It is used to return the Smallest of two values.


(ii) max(): It returns the Largest of two values.
(iii) round(): It is used to round off the decimal numbers to the nearest value.
(iv) ceil(): It is used to find the smallest integer value that is greater than or equal to
the argument or mathematical integer.
(v) abs(): It will return the Absolute value of the given value.

 10 marks
1. Describe various characteristics of Java.
Ans: A list of most important features of Java language is given below:
(1) Simple: Java is very easy to learn, and its syntax is simple, clean and easy to
understand. According to Sun Microsystem, Java language is a simple programming
language because Java syntax is based on C++ (so easier for programmers to
learn it after C++).
(2) Object-Oriented: Java is called an object-oriented programming language because it
is based on the concepts of objects and classes. Without the creation of object, it is
impossible to write any code in java.
(3) Platform-independent: Java is platform-independent because it is different from
other languages like C, C++, etc. which are compiled into platform-based machines.
Java executes under the control of a JVM, Java programs can run on any operating
system that provides a JVM.
(4) Portable: Java is portable because it facilitates you to carry the Java bytecode to any
platform. It doesn't require any implementation.
(5) Secured: Java is best known for its security. With Java, we can develop virus-free
systems. Java language provides default securities are classloader,bytecode verifier
and security manager.
(6) Robust: Robust simply means strong. Java is robust because:
It uses strong memory management.
There is a lack of pointers that avoids security problems.
(7) Architecture-neutral: Java is architecture neutral because there are no
implementation-dependent features, for example, the size of primitive types is fixed.
(8) Interpreted: Java is an interpreted language that is why it is slower than compiled
languages, e.g., C, C++, etc. Java bytecode is translated on the fly to native machine
instructions and is not stored anywhere.
(9) High-performance: Java is faster than other traditional interpreted programming
languages because Java bytecode is "close" to native code. It is still a little bit slower
than a compiled language (e.g., C++).
(10) Distributed: Java is distributed because it facilitates users to create distributed
applications in Java. RMI and EJB are used for creating distributed applications.
(11) Multi-threaded: A thread is like a separate program, executing concurrently. We can
write Java programs that deal with many tasks at once by defining multiple threads.
Threads are important for multi-media, Web applications, etc.
(12) Dynamic: Java is a dynamic language. It supports dynamic loading of classes. It
means classes are loaded on demand.

2. What is Operator? Explain any four types of operators with example.

Ans. (1) Operators are special characters used to instruct the Java compiler to perform an
operation on one, two, or three operands, and then return a result.
(2) Java operators take one or more arguments or operands and produce a value.
List of four operators in java are as follows:
(1) Unary Operator: The Java unary operators require only one operand. Unary operators are
used to perform various operations i.e.:
 incrementing/decrementing a value by one
 negating an expression
 inverting the value of a boolean

Java Unary Operator Example 2: ++ and --


public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=10;  
System.out.println(a++ + ++a);//10+12=22  
System.out.println(b++ + b++);//10+11=21  
 }
}  
Output:
22
21
(2) Java Arithmetic Operators: Java arithmatic operators are used to perform addition,
subtraction, multiplication, and division. They act as basic mathematical operations.
Java Arithmetic Operator Example
public class OperatorExample{  
public static void main(String args[]){  
int a=10;  
int b=5;  
System.out.println(a+b);//15  
System.out.println(a-b);//5  
System.out.println(a*b);//50  
System.out.println(a/b);//2  
System.out.println(a%b);//0  
}
}  
Output:
15
5
50
2
0
(3) Ternary Operator: Java Ternary operator is used as one line replacement for if-then-else
statement and used a lot in Java programming. It is the only conditional operator which takes
three operands.
Java Ternary Operator Example
public class OperatorExample{  
public static void main(String args[]){  
int a=2;  
int b=5;  
int min=(a<b)?a:b;  
System.out.println(min);  
}}  
Output:
2
(4) Assignment Operator: Java assignment operator is one of the most common
operators. It is used to assign the value on its right to the operand on its left.
Java Assignment Operator Example
public class OperatorExample{  
public static void main(String[] args){  
int a=10;  
a+=3;//10+3  
System.out.println(a);  
a-=4;//13-4  
System.out.println(a);  
a*=2;//9*2  
System.out.println(a);  
a/=2;//18/2  
System.out.println(a);  
}}
Output:  
13
9
18
9
3. Describe various data types in Java.
Ans. Data Types in Java
Data types specify the different sizes and values that can be stored in the variable. There
are two types of data types in Java:
1. Primitive data types: The primitive data types include boolean, char, byte, short, int,
long, float and double.
2. Non-primitive data types: The non-primitive data types include Classes, Interfaces,
and Arrays.
Java Primitive Data Types
In Java language, primitive data types are the building blocks of data manipulation.
These are the most basic data types available in Java language.
There are 8 types of primitive data types:
o boolean data type
o byte data type
o char data type
o short data type
o int data type
o long data type
o float data type
o double data type
Data Type Default Value Default size
boolean false 1 bit
char ‘\u0000’ 2 byte
byte 0 1 byte
short 0 2 byte
int 0 4 byte
long 0L 8 byte
float 0.0f 4 byte
double 0.0d 8 byte

(1) Boolean Data Type


The Boolean data type is used to store only two possible values: true and false. The
Boolean data type specifies one bit of information, but its "size" can't be defined
precisely.
(2) Char Data Type
The char data type is a single 16-bit Unicode character. The char data type is used to
store characters.
(3) Byte Data Type
The byte data type is an example of primitive data type. It isan 8-bit signed two's
complement integer. Its default value is 0.
(4) Short Data Type
The short data type is a 16-bit signed two's complement integer. Its
default value is 0.
(5) Int Data Type
The int data type is a 32-bit signed two's complement integer. Its default value is 0.
(6) Long Data Type
The long data type is a 64-bit two's complement integer. Its default value is 0. The long
data type is used when you need a range of values more than those provided by int.
(7) Float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating point.Its value range is
unlimited. It is recommended to use a float (instead of double) if you need to save
memory in large arrays of floating-point numbers. The float data type should never be
used for precise values, such as currency. Its default value is 0.0F.
(8) Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range
is unlimited. The double data type is generally used for decimal values just like float. The
double data type also should never be used for precise values, such as currency. Its
default value is 0.0d.

You might also like