Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Chapter 1 Java

Uploaded by

RKING
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Chapter 1 Java

Uploaded by

RKING
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Chapter – 1

5 Marks
1. What are the basic concepts of OOPs? Explain.
a) Object
Any entity that has state and behavior is known as an object. For
example, a chair, pen, table, bike, etc. It can be physical or logical.
An Object can be defined as an instance of a class. An object contains an
address and takes up some space in memory. Objects can communicate
without knowing the details of each other's data or code.
b) Class
Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an
individual object. Class doesn't consume any space.
c) Inheritance
inheritance is a fundamental concept in object-oriented programming
(OOP) that allows one class (subclass or derived class) to inherit
attributes and methods from another class (superclass or base class).
This promotes code reuse and enables hierarchical classification of
classes.
d) Polymorphism
If one task is performed in different ways, it is known as polymorphism.
For example: to convince the customer differently, to draw something,
for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve
polymorphism.
e) Abstraction
Java is a fundamental concept in object-oriented programming (OOP)
that focuses on hiding the implementation details of methods and
providing only the functionality that is necessary for the user. It allows
developers to create a simplified model of a complex system,
emphasizing what an object does rather than how it does it.
f) Encapsulation
Binding (or wrapping) code and data together into a single unit are
known as encapsulation. For example, a capsule, it is wrapped with
different medicines.
2. Explain features of java.
https://www.javatpoint.com/features-of-java
3. What is Encapsulation? Explain with example.
Encapsulation in Java is a fundamental principle of object-oriented
programming (OOP) that involves bundling the data (attributes) and
methods (functions) that operate on the data into a single unit (class). It
allows the attributes of a class to be hidden from other classes, ensuring
that they can only be accessed and modified through defined methods.
Encapsulation provides several benefits such as improved code
organization, reusability, and security.
4. Basic structure of java program.
https://i.postimg.cc/wMHxfKv1/structure.png
5. Explain decision making statements with syntax and example.
https://chatgpt.com/share/70daef51-2040-4c9a-864d-59fe7c9d821e

10 Marks
1) Explain type casting and type conversion with example
1. Type Casting
Type casting is the process of converting a variable from one data type
to another. In Java, there are two types of casting:
 Implicit Casting (Widening Casting): Automatically done by the
compiler when converting a smaller type to a larger type size.
 Explicit Casting (Narrowing Casting): Manually done by the
programmer when converting a larger type to a smaller size type.

Example of Implicit Casting (Widening Casting):


public class Main {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to double
System.out.println("Integer value: " + myInt);
System.out.println("Double value: " + myDouble);
}
}
Output:
Integer value: 9
Double value: 9.0
Example of Explicit Casting (Narrowing Casting):
public class Main {
public static void main(String[] args) {
double myDouble = 9.78;
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println("Double value: " + myDouble);
System.out.println("Integer value: " + myInt);
}
}
Output:
Double value: 9.78
Integer value: 9
2. Type Conversion
Type conversion is the process of converting data from one type to another. It
can be done manually or automatically and typically involves converting a
string to a numeric type or vice versa.
Example of Type Conversion:
public class Main {
public static void main(String[] args) {
String str = "123";
int num = Integer.parseInt(str); // Converting String to int
System.out.println("String value: " + str);
System.out.println("Integer value: " + num);
String str2 = String.valueOf(num); // Converting int to String
System.out.println("Converted String value: " + str2);
}
}
Output:String value: 123
Integer value: 123
Converted String value: 123
3) Explain the operator hierarchy write a java program for arithmetic
operators.
The order in which the arithmetic operations are executed in an expression is
called ‘hierarchy of operations’ or ‘operator precedence’. The rules governing
the precedence of operators generally follow the normal rules of algebra.
 The contents of all parentheses ( ) are evaluated first
 Exponents: ** (or ^ )
 Unary Plus and Minus: + and –
 Multiplication and Division: * and /
 Addition and Subtraction: + and –
Program
public class ArithmeticOperators {
public static void main(String[] args) {
int a = 10;
int b = 5;
// Addition
int sum = a + b;
System.out.println("Sum (a + b): " + sum);
// Subtraction
int sub = a - b;
System.out.println("Difference (a - b): " +sub);
// Multiplication
int mul = a * b;
System.out.println("Product (a * b): " +mul);
// Division
int div = a / b;
System.out.println("Quotient (a / b): " +div);

// Modulus
int remainder = a % b;
System.out.println("Remainder (a % b): " + remainder);

You might also like