0% found this document useful (0 votes)
118 views

OOP Java - IMP M 1

Uploaded by

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

OOP Java - IMP M 1

Uploaded by

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

OOPs in JAVA

rd
3 SEM Exam – Important Questions

MODULE – 1

1. Explain object oriented principles.


Ans:
1. Encapsulation:
 Encapsulation ensures data integrity by controlling access to the data through methods.
 It facilitates modular design, making it easier to manage and maintain code.
 Encapsulation also enables the implementation of access control mechanisms such as public,
private, and protected, allowing for better security and abstraction.
2. Inheritance:
 Inheritance supports the concept of code specialization, where subclasses can extend or
customize the behavior of the superclass.
 It promotes the principle of "is-a" relationship, where a subclass is said to be a specialized
version of its superclass.
 Inheritance facilitates the reuse of code, reducing redundancy and improving code
organization.
3. Polymorphism:
 Polymorphism enhances code flexibility and extensibility, allowing for the development of
more generic and adaptable software components.
 It encourages code decoupling by enabling interaction with objects at a higher level of
abstraction, reducing dependencies between classes.
 Polymorphism enables the development of frameworks and APIs that can accommodate a
variety of object types and behaviors.
4. Abstraction:
 Abstraction promotes code maintainability by isolating changes within the class
implementation, reducing the impact of modifications on other parts of the system.
 It facilitates code reuse by providing a clear and concise interface for interacting with objects,
regardless of their underlying implementation details.
 Abstraction enables developers to focus on the essential aspects of an object's behavior,
leading to more modular and understandable code.
2. Describe the meaning of each of the keyword in “public static void main” and write an
example program.
Ans:
1. public:
 public is an access modifier that specifies the visibility of the method. It means that the
method can be accessed and invoked from any other class.
2. static:
 static indicates that the method belongs to the class itself rather than to any instance
of the class. It allows the method to be called without creating an instance of the class.
3. void:
 void is the return type of the method. It means that the method does not return any
value after its execution.
4. main:
 main is the name of the method. It is the entry point for the Java program where the
execution begins.

In this example:
 public makes the main method accessible from any other class.
 static allows the main method to be called without creating an instance of the Main
class.
 void indicates that the main method does not return any value.
 main is the name of the method, which serves as the entry point of the program.
 String[] args is the parameter passed to the main method, which allows the program
to accept command-line arguments if needed.
 System.out.println("Hello, world!"); prints "Hello, world!" to the console when
the program is executed.
3. Explain different lexical issues in JAVA.
Ans:
Lexical issues: A token is the smallest program element which is recognized by the compiler
and Java tokens are called lexical issues. Java programs are a collection of whitespace,
identifiers, keywords, comments, separators, literals, and operators.
1. Whitespace:
 Whitespace refers to spaces, tabs, and newlines used to separate tokens in Java code.
 It helps improve code readability but is ignored by the compiler.
2. Identifiers:
 Identifiers are names given to classes, methods, variables, etc., in Java.
 They must start with a letter, underscore (_), or dollar sign ($), followed by letters,
digits, underscores, or dollar signs.
 Identifiers are case-sensitive and cannot be Java keywords.
3. Literals:
 Literals represent fixed values in Java code, such as numbers, characters, strings, etc.
 Examples include integer literals (e.g., 10), floating-point literals (e.g., 3.14), character
literals (e.g., 'A'), and string literals (e.g., "Hello").
4. Comments:
 Comments are used to annotate the code with explanations or notes for developers
or to temporarily disable code.
 Single-line comments start with //, and multi-line comments are enclosed between /*
and */.
5. Separators:
 Separators are special symbols used to separate tokens in Java code.
 Examples include semicolons (;) to terminate statements, commas (,) to separate
items in a list, and parentheses () to group expressions.
6. Java Keywords:
 Java has a set of reserved words known as keywords that have predefined meanings
and cannot be used as identifiers.
 Examples include public, static, class, if, else, while, etc.
4. Explain different types of arrays with simple program.
Ans:

One-Dimensional Arrays:

1. Declaration and Initialization:


 One-dimensional arrays are declared by specifying the data type followed by square brackets
([]), and then the array name.
 They can be initialized with values at the time of declaration or later using assignment
statements.
2. Accessing Elements:
 Individual elements in a one-dimensional array are accessed using their index.
 The index starts from 0 for the first element and goes up to one less than the length of the
array.
3. Traversing with for Loop:
 One-dimensional arrays are commonly traversed using a for loop to access each element
sequentially.
 The loop iterates from 0 to the length of the array minus one.
4. Example:
Multi-Dimensional Arrays:

1. Declaration and Initialization:


 Multi-dimensional arrays are declared by specifying multiple sets of square brackets, where
each set represents a dimension.
 They can be initialized with values at the time of declaration or later using assignment
statements.
2. Accessing Elements:
 Elements in a multi-dimensional array are accessed using multiple indices, each corresponding
to a specific dimension.
 The indices start from 0 for each dimension.
3. Traversing with Nested for Loops:
 Multi-dimensional arrays are commonly traversed using nested for loops to access each
element sequentially.
 The outer loop iterates over rows, and the inner loop iterates over columns.
4. Example:
5. Explain different promotion rules in JAVA.
Ans:
Java defines several type promotion rules that apply to expressions. They are as follows:

1. Promotion of byte, short, and char to int:


 Java promotes byte, short, and char values to int when used in expressions to ensure
consistent integer arithmetic.
2. Promotion to long:
 If one operand is long, the entire expression is promoted to long to avoid loss of
precision.
3. Promotion to float:
 If one operand is float, the entire expression is promoted to float to maintain floating-
point consistency.
4. Promotion to double:
 If any operand is double, the result is promoted to double to maintain maximum precision
in floating-point operations.

6. Explain the following operations with example. (i)<< (ii)>> (iii)>>> (iv)&
Ans:
(i) << (Left Shift):
 The left shift operator (<<) shifts the bits of a number to the left by a specified number of
positions.
(ii) >> (Signed Right Shift):
 The signed right shift operator (>>) shifts the bits of a number to the right by a specified
number of positions.

(iii) >>> (Unsigned Right Shift):


 The unsigned right shift operator ( >>>) shifts the bits of a number to the right by a specified
number of positions.
 Each shift to the right fills the leftmost positions with zeros, regardless of the sign bit.

(iv) & (Bitwise AND):


 The bitwise AND operator (&) performs a bitwise AND operation between corresponding bits
of two numbers.
 The result is 1 only if both bits are 1; otherwise, it's 0.
7. Demonstrate the working of enhanced for loop with an example program.
Ans: :
For-Each Loop in Java:

• type: Denotes the data type of elements within the collection or array.
• itr-var: Represents the name assigned to the iteration variable that sequentially
receives each element from the collection or array.
• The iteration variable (itr-var) receives elements individually, one after the other, in
the order they appear within the collection or array.
• The process traverses through the entire collection or array, iterating from the start
to the end.
• The collection being cycled through and providing elements to the iteration variable is
specified by collection
 Advantages:
 Simplifies iterating through arrays/collections.
 Eliminates the need for maintaining loop counters or indices.
 Improves code readability and reduces chances of off-by-one errors.
 Limitations:
 Cannot modify the structure of the array/collection during iteration (adding/removing
elements). Use an iterator for such cases.

8. Write a program to sort the elements using for loop.


Ans:
9. Explain four different types of if statements in JAVA with example.
Ans:
10. Demonstrate working of break with labels in JAVA.
Ans: Using break as a Form of Goto:

Example

You might also like