Part 2 | Cognizant Walk-In Drive 2025 🌸 | Must-Know Java Questions | Crack your First Job !
Автор: XAdmin
Загружено: 2025-07-11
Просмотров: 146
Описание:
In this video we are going to discuss about the java interview question for the cognizant walk-in Drive
Q14. What is a Class in Java?
Ans:
A class in Java is a blueprint, template, or prototype used to create objects.
It defines the properties (fields/variables) and behaviors (methods/functions) that the objects created from the class will have.
Think of a Class Like:
A blueprint for a house.
The blueprint defines:
How many rooms it will have
What the dimensions are
Where doors/windows will be
But it's not the house itself—you use the blueprint to build multiple houses.
In the same way:
A class defines the structure
An object is a real-world instance of that structure
Q15. What is an Object in Java?
Ans:
An object is a instance of a class.
It is a runtime entity that has:
State → represented by fields/variables
Behavior → represented by methods/functions
So, An object is an instance of a class. It occupies memory space and has a unique identity. You can create, modify, and use objects to perform tasks defined in the class.
Memory View:
When you create an object:
Student s1 = new Student("Ashish", 22);
new → allocates memory in the heap
s1 → reference to that memory (object)
Q16. What is encapsulation ?
Ans:
Encapsulation is one of the four main pillars of Object-Oriented Programming (OOP) and it means:
Hiding internal data and allowing access only through methods.
In other words:
It binds data (fields) and code (methods) together into a single unit (the class).
It protects the data from outside access or unauthorized modification.
Encapsulation is the process of wrapping data and methods into a single unit (class) and restricting direct access to some of the object's components.
It is implemented in Java using:
private → for data hiding
public → getter and setter methods for controlled access
Q17. Why main() method is public, static and void in java ?
Ans:
public static void main(String[] args)
public — Accessible by JVM
The main() method must be accessible to the Java Virtual Machine (JVM) from outside the class to start execution.
public means it is visible to all.
If it were private or protected, JVM wouldn’t be able to access it, and it would throw a runtime error.
Think of it like: A public gate to enter your program.
static — No Object Needed
main() is called by the JVM without creating an object of the class.
static allows the JVM to call it directly using the class name.
If main() were not static, the JVM would need to create an object first, which creates a chicken-and-egg problem — JVM needs to run main() to create objects, but main() needs an object to run.
Think of it like: A switch on the wall — you don’t need to build anything else to turn it on.
void — No Return Value
main() doesn’t return anything to the JVM.
It is the starting point of the program, not something that computes a result.
That's why its return type is void (i.e., it returns nothing).
Think of it like: A driver that starts your engine — it doesn’t give you a result; it just begins execution.
Q18. What is a Constructor in Java?
Ans:
A constructor in Java is a special method that is used to create and initialize an object of a class.
A constructor is a block of code similar to a method that is automatically called when an object is created.
Q19. What is difference between length and length() method in java?
Ans:
Key Difference
Feature
length
length()
Used With
Arrays
Strings (String class)
Type
Field / Property
Method
Syntax
array.length
string.length()
Returns
The number of elements in the array
The number of characters in the string
Parentheses
❌ No parentheses
✅ Must have parentheses ()
Q20. Difference between Character Constant and String Constant in Java?
Ans:
In Java, both character constants and string constants represent text, but they are fundamentally different in syntax, size, and type.
Q21. Explain about abstract classes in java?
Ans:
An abstract class in Java is a class that cannot be instantiated directly and is used to provide a base for other classes. It may contain:
Abstract methods (without body)
Concrete methods (with implementation)
Fields (variables) and constructors
An abstract class is a class declared using the abstract keyword. It may contain incomplete methods that subclasses must override.
Q22. Can we create constructor in abstract class?
Ans:
Yes, you can create a constructor inside an abstract class in Java.
Even though you cannot instantiate an abstract class directly, its constructor is still called when a subclass object is created.
Q24. Why Allow Constructors in Abstract Classes?
Ans:
To initialize common fields shared by all subclasses
To perform setup tasks for subclasses
To allow constructor chaining through super()
#java #javainterview #javainterviewquestions
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: