0% found this document useful (0 votes)
18 views6 pages

JAVA1 Mid1 Answers

The document provides an overview of command-line arguments in Java, explaining how they can be accessed and utilized in a program through examples. It also introduces Object-Oriented Programming (OOP), detailing its four main principles: encapsulation, abstraction, inheritance, and polymorphism, along with their definitions and purposes. Additionally, it touches on various Java concepts such as overloaded constructors, argument passing, method overriding, the 'this' keyword, access control, and the role of the 'static' keyword.

Uploaded by

lakshmimahaa999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

JAVA1 Mid1 Answers

The document provides an overview of command-line arguments in Java, explaining how they can be accessed and utilized in a program through examples. It also introduces Object-Oriented Programming (OOP), detailing its four main principles: encapsulation, abstraction, inheritance, and polymorphism, along with their definitions and purposes. Additionally, it touches on various Java concepts such as overloaded constructors, argument passing, method overriding, the 'this' keyword, access control, and the role of the 'static' keyword.

Uploaded by

lakshmimahaa999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

JAVA

mid-1 answers

Long answer questions


UNIT - I

1. How can command-line arguments be passed to a Java program? Provide an


example of how they can be accessed within a program.

COMMAND LINE ARGUMENTS:

 The java command line arguments I a na arguments passed at the time of


running java program.
 The arguments passed from console can be received in the java program and it
can be an input.

Eg:

Class A
{
Public static void main(String args[])
{
for(int i=0;i<args.length;i++)

Syatem.out.println(aygs[i]);

Output:

Compile by>javac A.java

Run by> java A

jaiswal

Command line arguments are parameters that are passed to the application program at
the time of execution. The parameters are received by args array of string type.
The first argument is stored at args[0]

The second argument is stored at args[1] and so on….

Example 1:

class Commarg

public static void main(String args[])

int i=0,l;

l=args.length;

System.out.println("Number of arguments are:"+l);

while(i<1)

System.out.println(args[i]);

i++; }

Ex:

C:..\>java Commarg hello how are u

output:

number of arguments are : 4

hello

how

are

Note: From the above output it is clear that hello is stored at args[0]
Position and so on…..

Example-2:

Program to add two no’s using command line arguments.

class CmdAdd2

public static void main(String args[])

// if two arguments are not entered then come out

if(args.length!=2)

System.out.println("Please enter two values....");

return;

int a=Integer.parseInt(args[0]);

int b=Integer.parseInt(args[1]);

int c=a+b; System.out.println("The Result is:"+c);

To run the program:

> java CmdAdd2 10 20

2. What is Object-Oriented Programming (OOP)? Explain its four main principles.


OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or methods that


perform operations on the data, while object-oriented programming is about
creating objects that contain both data and methods.

Object-oriented programming has several advantages over procedural


programming:

3. OOP is faster and easier to execute


4. OOP provides a clear structure for the programs
5. OOP helps to keep the Java code DRY "Don't Repeat Yourself", and
makes the code easier to maintain, modify and debug
6. OOP makes it possible to create full reusable applications with less
code and shorter development time
7.

Object-Oriented Programming (OOP) is a programming paradigm that uses "objects"


to represent data and methods to operate on that data. It focuses on organizing
software around these objects rather than functions or logic. OOP helps to model real-
world entities more intuitively, making software more modular, reusable, and easier to
maintain. The four main principles of OOP are:

1. Encapsulation:
o Definition: Encapsulation is the concept of bundling the data (attributes) and
methods (functions) that operate on the data into a single unit called a class. It
also involves restricting direct access to some of an object's components,
which is typically achieved through access modifiers like private, protected,
and public.
o Purpose: This principle helps protect the internal state of an object from
unintended interference and misuse, making the object’s behavior more
predictable and manageable. It also helps in hiding the complexity of the
implementation, exposing only what is necessary.
2. Abstraction:
o Definition: Abstraction involves simplifying complex systems by modeling
classes based on the essential properties and behaviors an object should have,
without detailing the internal implementation. It focuses on what an object
does rather than how it does it.
o Purpose: This principle allows programmers to work at a higher level of
complexity without needing to understand the lower-level details. By
providing a clear interface and hiding implementation details, abstraction
helps in reducing complexity and improving code readability.
3. Inheritance:
o Definition: Inheritance is a mechanism that allows one class (the subclass or
derived class) to inherit attributes and methods from another class (the
superclass or base class). This promotes code reuse and establishes a
hierarchical relationship between classes.
o Purpose: Inheritance enables new classes to be created based on existing
classes, allowing for the extension and customization of inherited behavior
without altering existing code. This helps in reducing redundancy and
promoting a more organized class structure.
4. Polymorphism:
o Definition: Polymorphism allows objects to be treated as instances of their
parent class rather than their actual class. It also enables a single method or
property to perform different functions based on the object’s actual class.
o Purpose: Polymorphism enhances flexibility and interoperability in code. It
allows for the implementation of a common interface while providing specific
behaviors for different data types or classes, thus enabling the same operation
to behave differently based on the object it is applied to.

8. What is the syntax and purpose of a do-while loop? In what scenario would you
prefer it over a while loop?

9. What is type casting? Differentiate between implicit and explicit type casting
with examples.

10. Explain the use of the final keyword in Java. What are its implications when
applied to variables, methods, and classes?

11. What are literal constants in Java? Provide examples.

UNIT -II

12. Explain the concept of overloaded constructors. Provide an example where


multiple constructors are used in a class.
13. Discuss the difference between passing arguments by value and by reference.
How does Java handle argument passing?
14. What is method overriding in Java? How does it differ from method
overloading?
15. What is the this keyword in Java? Explain its significance with examples.
16. Discuss the concept of access control in methods. How do access modifiers affect
method visibility
17. What is the role of the static keyword in methods? How do static methods
differ from instance methods?

You might also like