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

JAVA UNIT-2 2MARKS

The document provides a comprehensive overview of key concepts in Java, including definitions and explanations of objects, classes, constructors, access modifiers, and methods. It covers various topics such as class creation, variable types, method overloading and overriding, recursion, and the use of the 'this' keyword. Additionally, it includes examples of class declarations with different modifiers and discusses the differences between reference and content equality.

Uploaded by

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

JAVA UNIT-2 2MARKS

The document provides a comprehensive overview of key concepts in Java, including definitions and explanations of objects, classes, constructors, access modifiers, and methods. It covers various topics such as class creation, variable types, method overloading and overriding, recursion, and the use of the 'this' keyword. Additionally, it includes examples of class declarations with different modifiers and discusses the differences between reference and content equality.

Uploaded by

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

UNIT-2 (2 MARKS)

Q1. What is an object in Java?


Answer: An object in Java is the physical as well as a logical entity, whereas, a class in Java is a logical
entity only.
An object is a real-world entity. An object is a runtime entity.

Q2. What is a class in Java?


Answer: A class is a group of objects which have common properties.
It is a template or blueprint from which objects are created.
It is a logical entity. It can't be physical.
Classes are user-defined data type.
A class is declared by use of the class keyword.
A class in Java can contain: 1. Fields 2. Methods 3. Constructors 4. Blocks 5. Nestedclass and interface..

Q3. How are classes created in Java?


Answer: A class declaration starts with the Access modifier. It is followed by keyword class, which is
followed by the class name or identifier.
The body of class is enclosed between a pair of braces { }.

Q4. What are class modifiers in Java?


Answer: Class modifiers are used to control the access to class and its inheritance characteristics.
Java consists of packages and the packages consist of sub-packages and classes
Packages can also be used to control the accessibility of a class
These modifiers can be grouped as (a) access modifiers and (b) non-access modifiers.

Q5. Give an example of class declaration without modifier?


Answer: A class without a modifier is declared with keyword class followed by name/identifier ofclass,
and name is followed by a pair of braces { }.
Example:-
package pack1;
class Myclass{
/* class body */
}.

Q6. How are objects created in Java?


Answer: Using the "new" keyword followed by the class name and parentheses .
Syntax: class_name object_name;
For Example A Class defined as
public class Farm
{
int length;
int width;
}

Q7. What is access modifier in java?


Answer: An access modifier in java specifies accessibility (scope) of instance variables, methods, classes,
constructor etc.  In Java, There are three access specifiers are permitted:
1. public 2. protected 3. private 4. default or No modifier.
Q8. Can a class be instantiated multiple times?
Answer: Yes, creating multiple objects.

Q9.what are the types of variables?


Answer: There are two type of variable
1.Non Static variables.
(a) Instance variables: These variables are individual to an object and an object keeps a copy of these
variables in its memory.
(b) Local variables: These are local in scope and not accessible outside their scope.
2. Class variables (Static Variables): These variables are also contains as static keyword. The values of
these variables are common to all the objects of the class..

Q10. Explain private,protected,public,default ?


Answer:
private: private has scope within the class only
public: public has scope useful to every where
protected: protected has scope within the package and, child class of same package and child class of
other package
Default or No modifier: if you don’t use any modifier it is treated as default it is accessible only within
the package.

Q11. What is a constructor in java?


Answer:
A constructor is a block of codes similar to the method. It is called when an instance of the class is created.
At the time of calling constructor, memory for the object is allocated in the memory.

Q12. What are the two types of constructors?


Answer: There are two types of constructors in Java: 1. Default constructor (no-arg constructor) 2.
Parameterized constructor.

Q13. What is default constructor?


Answer: A constructor is called "Default Constructor" when it doesn't have any parameter.
The default constructor provided by java compiler, is a no-argument constructor with empty body.

Q14.What is final class in java?


Answer:
Final Class
A class that is declared with the final keyword is known as the final class.
A final class can’t be inherited by subclasses.
By use of the final class, we can restrict the inheritance of class.

Q15. What is the syntax for final clss?


Answer: Syntax:
accessModifier final class className
{
// Body of class
}.

Q16. What is the final method in java?


Answer: Final Method
We can declare a method as final, once you declare a method final it cannot be overridden. So, you cannot
modify a final method from a sub class.
The main intention of making a method final would be that the content of the method should not be
changed by any outsider.

Q17. What is the syntax for final method?


Answer: Syntax:-
final accessmodifier datatype methodname(Parameter….)
{
// Method body
}.

Q18. What is call by value?


Answer: Call by Value:
Call by Value means calling a method with a parameter as value. Through this, the argument value is
passed to the parameter.
In call by value, the modification done to the parameter passed does not reflect in the caller's function.

Q19. What is recursion?


Answer: A method calling itself is called recursion.

Q20i. What is the difference between static and instance methods?


Answer: Static methods belong to the class; instance methods belong to objects.

Q21. What is call by reference?


Answer
Call by Reference means calling a method with a parameter as a reference. Through this, the argument
reference is passed to the parameter.
In call by reference the object will be passed to the method as an argument..

Q22. What is the purpose of the "this" keyword?


Answer: this keyword in Java is a reference variable that refers to the current object of a method or a
constructor.
this keyword in Java is to remove the confusion between class variable and parameters that have same
names.

Q23. What is nested class in java?


Answer:
The Java programming language allows you to define a class within another class. Such a class is called a
nested class.
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they
are declared private.

Q24. What is the difference between "==" and ".equals()"?


Answer: "==" checks reference equality; ".equals()" checks content equality.

Q25. Give an Example of class Declarations with a modifier?


Answer: A class with modifier is declared as follows
package pack1;
public class Myclass{
/* class body */
}
Q26. Give an Example of class Declarations with private and protected ?
Answer: The modifier class with private and protected is used only for nested class.
package pack1;
class X{
Statements;
private class Myclass
{ // Class body
}
}
Q27.Give an Example of class Declarations with the final modifier.

Answer: Class with the final modifier are declared as follows


package pack1;
final class Myclass{
// Class body
}

Q28. Give an Example of class Declarations Class with the abstract modifier ?
Answer: Class with the abstract modifier are declared as follows
package pack1;
abstract class Myclass{
abstract void display();
}
A class declared abstract must have one or more abstract methods as its members that is
methods without a body; it is only the header of a method followed by semicolon.

Q29. What is the method in java?


Answer: Methods in JAVA
Method is a way to perform some task.
A method is a block of code or collection of statements or a set of code grouped together to perform a
certain task or operation. The method in Java is a collection of instructions that performs a specific task.

Q30. How can we declare methods?


Answer: Method Declaration
The method declaration provides information about method attributes, such as visibility, return-type, name,
and arguments (Parameter List).
It has six components that are known as method Declaration.

Q31. What is method signature?


Answer: Method Signature
Every method has a method signature. It is a part of the method declaration. It includes the method name
and parameter list

Q32.Can methods be final?


Answer: Yes, preventing overriding
Q33. What is a constructor?
Answer: A constructor method is automatically called whenever a new object of the class is
constructed. It creates and initializes the Object.
A constructor method has the same name as the name of class to which it belongs. It has no
type and it does not return any value. It only initializes the object.

Q34. What is constructor overloading?


Answer: Constructor overloading in Java is a technique that enables a single class to have more
than one constructor that varies by the list of arguments passed.
The constructor overloading enables the accomplishment of static polymorphism.

Q35. What is recursion in java?


Answer: Recursion in java is a process in which a method calls itself continuously. A method in
java that calls itself is called recursive method.

Q36. What is the nesting of method?


Answer: A method can be called by using only its name by another method of the same
class that is called Nesting of Method.

Q37. What is method overriding?


Answer: If subclass (child class) has the same method as declared in the parent class, it is known as
method overriding in Java.
In other words, If a subclass provides the specific implementation of the method that has been declared by
one of its parent class, it is known as method overriding.

Q38. What is method overloading in java?


Answer: If a class has multiple methods having same name but different in parameters, it is known as
Method Overloading. Method Overloading is one way of achieving polymorphism in JAVA

You might also like