JAVA1 Mid1 Answers
JAVA1 Mid1 Answers
mid-1 answers
Eg:
Class A
{
Public static void main(String args[])
{
for(int i=0;i<args.length;i++)
Syatem.out.println(aygs[i]);
Output:
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]
Example 1:
class Commarg
int i=0,l;
l=args.length;
while(i<1)
System.out.println(args[i]);
i++; }
Ex:
output:
hello
how
are
Note: From the above output it is clear that hello is stored at args[0]
Position and so on…..
Example-2:
class CmdAdd2
if(args.length!=2)
return;
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
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?
UNIT -II