Part 5
Java interview questions and answers on oops covered in this post:
What is constructor chaining?
- One constructor can call another constructor internally or
explicitly is called constructor chaining.
- Constructor can call another constructor using this() and
super().
Can we override or overload the constructor?
- We cannot override the constructor but we can overload the
constructor.
- Constructor overriding is not possible because the
constructor must have the same name of the class and doesn’t have any return
type. But for overriding signature must be same. So ultimately overriding is
not possible.
- Constructor overloading is possible because the constructor
name is the same as class name and type of parameter and no of parameters
are different so constructor overloading is allowed.
Can a constructor have a return type? If no can you explain
why?
- Constructor is specially used to initialize the object, not
the business logic processing.
- If the constructor doesn’t have any business logic and
nothing is going to return so constructor doesn’t have a return type.
Can constructor have access specifiers? Which access specifiers are
applicable to a constructor?
- Yes of course. Constructors can have access specifiers.
- Following access specifiers are applicable to the
constructors.
1. Public
2. Private
3. Protected
4. Default
- Following access specifiers are not applicable to the
constructors.
1. Abstract
2. Final
3. Native
4. Static
5. Synchronized.
Can constructor declare exception with throws keyword?
- Yes we can declare an exception in the constructor definition
with the throws keyword.
- We can declare both exceptions checked exceptions and
unchecked exceptions in the constructor definition.
- For your reference, I have provided multiple scenarios in the
below example.
package simplifiedjava.crackedInterview;
import
java.io.FileNotFoundException; import java.io.IOException; public class ExceptionThrowsInConstructor { public ExceptionThrowsInConstructor() throws FileNotFoundException{ } public ExceptionThrowsInConstructor(int num) throws IOException{ } public ExceptionThrowsInConstructor(String str) throws ArrayIndexOutOfBoundsException, NullPointerException{
}
} |
What is the default constructor and what is the difference between the
default constructor and no-arg constructor and parameterized
Constructor?
- Default constructor is a type of constructor provided by the
compiler when there is no constructor declared in the class.
- Default constructor is a non-parameterized constructor.
- Default constructor doesn’t have any parameters. Access
specifiers are default.
- No-arg constructor doesn’t have any parameter. But we can
declare no-arg constructor as public, private, protected and default.
- Parameterized constructor must have at least one parameter.
The parameterized constructor can be declared as public, private, protected
and default.
Can we declare multiple constructors in a single class?
- Yes. One class can have multiple constructors.
- If one class has multiple constructors then it is called constructor
overloading.
What is an abstract class? Can we instantiate abstract class?
- A class declared with an abstract keyword is called an
abstract class.
- If you declare a class as an abstract then we can declare the
abstract method and concrete method as well.
- If there is a single abstract method declared inside a class
then it is mandatory to declare the class as an abstract class.
- It is not mandatory to declare at least a single abstract
method inside an abstract class. In short, if you already declared class as
abstract then it is not mandatory to the declared abstract method.
Can I declare a final and abstract method in abstract class?
- You cannot declare final and abstract at a time. Either final
or abstract method can be declared.
- You can declare the final method or abstract method inside
the abstract class individually.
Can we declare a class as Abstract without having a single abstract
method?
- You can declare an abstract class without a single abstract
method declared inside an abstract class.
- Make a note if you declared the already abstract method then it is mandatory to declare the class as abstract class.
- Java interview questions and answers all MNC - Click here
- Basic core java interview questions and answers for freshers - Click here
- Core java interview questions for 3 years experience - Click here
- Core java interview questions and answers for 3-5 years exp - Click here
- Core java interview questions and Answers for 5 - 7 Years exp - Click here
- Basic Java Interview Questions and Answers - Click here
- Java interview questions and answers on oops - Click here
- Java interview questions and answers on Strings - Click here
- Java interview questions on exception handling - Click here
- Interview questions on multithreading in java for experienced - Click here
- Interview questions on serialization in java for experienced - Click here
- Interview questions on inner class in java for experienced - Click here
- Interview questions on Collections in java for experienced - Click here
Upcoming questions in next post:
51. Can I declare a final and abstract method in abstract class?
52. Can we declare a class as Abstract without having a single abstract method?
53. What is the difference between the final method and the abstract method?
54. What is the difference between Interface and Abstract Class? Can you tell me one real-time example where Interface and abstract class implementation is the best practice?
55. Will the code compile if I have declared a single abstract method in class but doesn't declare the class as an abstract class?
56. Can we declare static method and static variables in abstract class?
57. Can we declare the concrete method in abstract class?
58. Can we use static members inside the non-static area?
59. What is static import?
60. Where the static variables are stored in java?
Thank you techies for visiting this blog. I hope you enjoyed this blog and got more technical knowledge. I have tried to cover all types of questions and provided examples tested on eclipse as much as I can. Guys, please don’t just mug up the questions and answers. Try to clear your concepts with examples. Try to write a code on eclipse once you read the concepts. It will help you to memorize the concepts for a very long time. Simultaneously you will be prepared for interview programs as well. It will help you to survive in the IT sector for a long time. It may be easy to crack an interview but it's really tough to survive in the IT industry with inadequate knowledge and skills. I have collected all the questions from actual interviews attended by my friends, colleagues, college mate and classmate. I have covered frequently asked questions as well as challenging questions. I have included many programs to understand the concept thoroughly. I will try to explain the concept with the help of a real-time program in eclipse. You can share more questions which are not covered in this blog or post. Always welcome your suggestions and queries. I will definitely try to resolve it. Please comment your queries and new set of questions under the comment sections. I will create a new post for those questions.
My total experience is 10. Initially I had worked on some support
projects and then I moved to java projects. I had worked with many
multi-national companies like R-Systems, Tata Consultancy Services,
Cybage Softwares. Fortunately, TCS and Cybage has given
me an opportunity to take interviews for experienced candidates. I
have conducted more than 1000 interviews by now. Mock sessions will be conducted for minimal charges. I will guide you personally on how to crack interviews. All sessions will be online sessions only. My interview book will be provided free of cost if you enroll for personal training. Once you have done practice then I assured you will definitely crack almost all java interviews. I have published my book named with "All MNC Java Interview" which is available on amazon. You can go through it or you can directly contact to me if you are interested to read the book. I will give you special discount. I have covered near about 550 questions and answers with program tested on eclipse and necessary diagram. I have covered interview questions asked in all reputed MNC like TCS, Infosys, Capgemini, Tech Mahindra, Cognizant, City Bank, Barclays, Amdocs, Mindtree etc. My purpose behind this book is, help you to get a job in your dream company. I can understand this is a struggling period for job seekers and freshers. Everybody must have gone from this phase. so never give up. Keep upgrading yourself. I am sure you will get a job in your dream company. All the best!!! Please reach out to me for personal training for interview preparation.
You can reach out to me at mncjavainterview@gmail.com.
|
0 Comments