OOP Java - IMP M 1
OOP Java - IMP M 1
rd
3 SEM Exam – Important Questions
MODULE – 1
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:
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.
• 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.
Example