Core Java Unit-I
Core Java Unit-I
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.
5 marks
1. Explain the structure of Java program ?
(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:
class Main {
public static void main(String[] args) {
case 29:
size = "Small";
break;
case 42:
size = "Medium";
break;
case 48:
size = "Extra Large";
break;
default:
size = "Unknown";
break;
}
System.out.println("Size: " + size);
}
}
Output:
Size: Large
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.
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.
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