Java Preparation Unit - 1
Java Preparation Unit - 1
Unit-1
Basic Syntactical Constructs in Java
2 Marks
1. Enlist any two logical operators and two bitwise operators.
OR
Enlist the logical operators in Java.
OR
Enlist the Relational /comparison operators in Java.
Ans) Logical Operators:
1. AND Operator (&&): Returns true if both operands are true.
2. OR Operator (||): Returns true if at least one of the operands is true.
3. NOT Operator (!): Returns true if the operand is false and vice versa.
Bitwise Operators:
1. Bitwise OR (|): Sets each bit to 1 if either of the corresponding bits of the two
operands is 1.
2. Bitwise AND (&): Sets each bit to 1 if both corresponding bits of the two
operands are 1.
3. Bitwise XOR (^): Sets each bit to 1 if only one of the corresponding bits of the
two operands is 1.
4. Bitwise Complement (~): Flips the bits of its operand.
5. Bitwise Left Shift (<<): Shifts the bits of the first operand to the left by the
number of positions specified by the second operand.
6. Bitwise Right Shift (>>): Shifts the bits of the first operand to the right by the
number of positions specified by the second operand.
Comparison Operators:
Example:
public class TypeCastingExample
{
public static void main(String[] args)
{
int intValue = 10;
double doubleValue = intValue;
System.out.println("Implicit Type Casting: " + doubleValue);
6. Write all primitive data type available in Java with their storage Sizes in byte.
Ans)
Example:
public class TypeCastingExample
{
public static void main(String[] args)
{
int intValue = 10;
double doubleValue = intValue;
System.out.println("Implicit Type Casting: " + doubleValue);
2. Inheritance:
• Inheritance allows one class (subclass or derived class) to inherit the properties
and behaviors (methods) of another class (superclass or base class).
• It promotes code reusability and allows the creation of a hierarchy of classes.
3. Polymorphism:
• Polymorphism means the ability to take many forms. In Java, polymorphism
allows objects to be treated as instances of their superclass, enabling different
behaviors to be invoked through a single interface.
• It includes method overriding and method overloading.
4. Platform Independence:
• Java programs are compiled into platform-independent bytecode, which can be
executed on any platform with a Java Virtual Machine (JVM).
• This is achieved through the "Write once, run anywhere" (WORA) principle.
5. Portability:
• Portability refers to the ability of Java programs to run on any platform with a
compatible JVM without modification.
• Java achieves portability by providing a standard set of libraries and APIs.
6. Robust:
• Java is robust due to its strong memory management, automatic garbage
collection, exception handling mechanism, and strict compile-time and runtime
checking.
• It prevents memory leaks, buffer overflows, and other common programming
errors.
7. Supports Multithreading:
• Java supports multithreading, allowing concurrent execution of multiple threads
within a single program.
• Multithreading enhances responsiveness and improves performance in
applications that need to perform multiple tasks simultaneously.
9. Secure:
• Java's security features include bytecode verification, classloader architecture,
access control mechanisms, and the Java Security Manager.
• These features prevent unauthorized access to resources and protect against
malicious code.
11. Dynamic:
• Java is dynamically extensible, meaning classes can be loaded on-demand at
runtime.
• This enables dynamic linking of libraries and the creation of flexible, adaptable
applications.
2. private:
• The private access specifier restricts access to the element only within the
same class where it is declared.
• Private members are not visible to any other class, even within the same
package.
3. default (Friendly):
• The default access specifier, also known as the package-private specifier,
allows access to the element within the same package but not from outside
the package.
• If no access specifier is specified, Java considers it as default access.
• Default members are visible only within the same package and not to
classes outside the package.
4. protected:
• The protected access specifier allows access to the element within the
same package and by subclasses (inheritance), regardless of their location.
• Protected members are visible to subclasses and classes within the same
package.
4. Explain switch case and conditional operator in java with suitable example.
Ans) Switch-case Statement:
• The switch-case statement is used to select one of many code blocks to be
executed based on the value of a variable or expression.
• It provides a cleaner and more readable way to write multiple if-else statements
when comparing the value of a single variable against multiple possible values.
• The switch-case statement consists of multiple case blocks and an optional default
block.
• When a switch-case statement is executed, the value of the expression inside the
switch is compared with the values of each case block.
• If a match is found, the corresponding case block is executed. If no match is found,
the optional default block is executed (if present).
Syntax:
switch (expression) {
case value1:
// Code block to execute if expression matches value1
break;
case value2:
// Code block to execute if expression matches value2
break;
// Additional case blocks for other values
default:
// Code block to execute if no case matches expression
break;
}
Example:
public class SwitchCaseExample {
public static void main(String[] args) {
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Invalid day");
break;
}
}
}
Conditional (Ternary) Operator:
• The conditional operator, also known as the ternary operator, is a shorthand way
of writing an if-else statement.
• It takes three operands: a boolean expression followed by a question mark (?), a
value if the expression is true, and a value if the expression is false.
• It is used to make decisions within a single expression rather than using multiple
lines of code.
• The conditional operator is useful when you want to assign a value to a variable
based on a condition in a concise manner.
Syntax:
variable = (condition) ? expression1 : expression2;
Example:
public class ConditionalOperatorExample
{
public static void main(String[] args) {
int x = 5;
int y = (x < 10) ? 10 : 20;
5. Explain the concept of platform independence and portability with respect to Java
language.
Ans) Platform Independence in Java:
1. Java achieves platform independence by compiling the source code into bytecode,
rather than directly into machine code.
2. Bytecode is a platform-independent intermediate code.
3. The Java Virtual Machine (JVM) interprets the bytecode at runtime and translates
it into machine code suitable for the host platform.
4. This bytecode interpretation allows Java programs to run on any platform with a
compatible JVM, without the need for recompilation.
5. As a result, Java follows the "Write once, run anywhere" (WORA) principle,
enabling developers to write code on one platform and execute it on multiple
platforms without modification.
Portability in Java:
1. Portability in Java refers to the ability of Java programs to be easily transferred
from one computer system to another, without requiring modifications to the
code.
2. Java achieves portability by providing a standard set of libraries and APIs that
abstract away the underlying hardware and operating system details.
3. Java's platform-independent bytecode and the JVM enable Java programs to run
on different hardware architectures and operating systems seamlessly.
4. Developers can write Java applications once and deploy them across various
platforms, reducing the effort and cost associated with maintaining multiple code
bases for different platforms.
Common Example:
public class LogicalOperatorsExample {
public static void main(String[] args) {
int x = 5;
int y = 10;
boolean isEven = (x % 2 == 0);
boolean isPositive = (y > 0);