0 votes
74 views
in JAVA by
Q.01

 a- Explain object oriented principles.

 b- Describe the meaning of each of the keyword in “public static void main”

 c- Explain different lexical issues in JAVA.

3 Answers

0 votes
by

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects," which can contain data and code. The four key principles of OOP are:

1. Encapsulation

  • Definition: Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit or class. It restricts direct access to some of the object's components, which is known as data hiding.
  • Example: A Car class might encapsulate properties like speed and fuel, and methods like accelerate() and brake(). Access to the speed property can be controlled using methods like getSpeed() and setSpeed().

2. Abstraction

  • Definition: Abstraction focuses on exposing only the essential features of an object while hiding the unnecessary details. It helps in reducing complexity and making the system easier to understand.
  • Example: Consider an Account class that exposes the methods deposit() and withdraw(). The complex details of how these methods work internally (like database transactions) are hidden from the user.

3. Inheritance

  • Definition: Inheritance allows a new class (child or subclass) to inherit properties and behaviors (methods) from an existing class (parent or superclass). This promotes code reusability.
  • Example: A Vehicle class can be a parent class, and Car and Bike can inherit common attributes like engine and methods like start() from Vehicle. Additional unique attributes can be added to Car and Bike.

4. Polymorphism

  • Definition: Polymorphism allows objects of different classes to be treated as objects of a common superclass. It also allows methods to have different implementations based on the object that calls them.
  • Types:
    • Compile-time (Method Overloading): Multiple methods with the same name but different parameters.
    • Run-time (Method Overriding): A subclass can provide its own implementation of a method that is already defined in its superclass.
  • Example: A Shape class might have a method draw(). Subclasses like Circle, Square, and Triangle can override the draw() method to provide their specific implementations.

These principles are fundamental to creating modular, reusable, and scalable software using OOP techniques.

0 votes
by

In Java, the signature of the main method is usually written as public static void main(String[] args). Each keyword in this signature has a specific purpose:

1. public

  • Meaning: This is an access modifier. It means that the main method is accessible from anywhere. In the context of the Java application, the JVM (Java Virtual Machine) needs to call this method from outside the class, so it must be public to allow such access.

2. static

  • Meaning: The static keyword means that the main method belongs to the class rather than an instance of the class. This allows the JVM to call the main method without creating an instance of the class, which is necessary since main is the entry point of the program.

3. void

  • Meaning: This specifies that the main method does not return any value. It simply executes the statements within the method, and no value is returned to the calling code (which, in this case, is the JVM).

4. main

  • Meaning: main is the name of the method. It's a special method name in Java because the JVM looks for this method as the entry point to start program execution. When a Java application is run, the JVM calls the main method to begin execution.

Full Signature Breakdown:

  • public static void main(String[] args): The main method is public so the JVM can access it, static so it can be invoked without creating an object, returns nothing (void), and takes an array of String arguments (args), which can be passed from the command line.
0 votes
by

Lexical Issues in Java

1. Incorrect Use of Reserved Keywords

Issue: Java has reserved keywords (e.g., ifclassstatic) that cannot be used as identifiers such as variable or method names.

int static = 5;  // Incorrect: 'static' is a reserved keyword

Solution: Avoid using reserved keywords as identifiers.

2. Case Sensitivity

Issue: Java is case-sensitive, meaning variableVariable, and VARIABLE are different identifiers.

int count = 5;
int Count = 10;  // Different from 'count'

Solution: Be consistent with case when naming variables and methods.

3. Unicode Characters in Identifiers

Issue: Java allows Unicode characters in identifiers, which can lead to confusion with visually similar characters.

int  = 10;  // Alpha in Greek letters
int α = 20;  // Visually similar, but different

Solution: Avoid using confusing Unicode characters for better readability.

4. Literals and Escape Sequences

Issue: Improper use of escape sequences in string or character literals can cause errors.

char ch = '\';  // Incorrect: improper use of escape character

Solution: Properly escape characters using \\ for backslashes and \' for single quotes.

5. Commenting Issues

Issue: Errors arise if comments are not closed properly, especially with multi-line comments.

/* This is a comment that doesn't end properly
int num = 5;  // Error: The multi-line comment isn't closed

Solution: Ensure all comments are properly closed.

6. String Literals Across Multiple Lines

Issue: Java doesn't allow multi-line string literals without concatenation.

String text = "This is a 
string";  // Incorrect

Solution: Use concatenation for long strings:

String text = "This is a " +
               "string";

7. Malformed Numeric Literals

Issue: Numeric literals must follow specific rules in Java, such as octal numbers starting with 0.

int num = 0123;  // Interpreted as octal (not 123)
double value = 1.23.45;  // Incorrect

Solution: Use correct formats for numeric literals.

8. Character Literals

Issue: Character literals must consist of a single character enclosed in single quotes.

char ch = 'ab';  // Incorrect: multiple characters
char ch = '';    // Incorrect: empty character literal

Solution: Ensure character literals contain only a single character.

9. End of Line (EOL) or Newline Issues

Issue: Errors occur if tokens like string literals or comments are not terminated before the end of a line.

System.out.println("Hello World  // Incorrect: Unclosed string literal

Solution: Ensure all tokens are properly closed before the end of a line.

10. Whitespace Issues

Issue: Java generally ignores extra spaces and newlines, but splitting tokens like operators across lines can cause readability issues.

int a = 5 + 
10;  // Valid, but can reduce readability
Welcome to VTU, where you can ask questions and receive answers from other members of the community.
VTU 4th sem Results Results updated on: September 12, 2024 @ 12:30 PM
...