Basic Java Interview Questions and Answers covered in this post:
Can we declare constructor, methods and fields inside the ENUM definition?
- Yes. We can declare fields, methods and constructors inside
ENUM.
- Please refer to the below example for enum
implementation.
- Enum constructors must be declared as private if you don’t declare private
then by default enums constructor is private so cannot declare public or
protected.
package simplifiedjava.crackedInterview; public class EnumConstructorMethodsDemo { public enum Months{ JANURAY(1),FEBRUARY(2),MARCH(3),APRIL(4),MAY(5),JUNE(6),JULY(7),AUGUST(8),SEPTEBER(9),OCTOBER(10),NOVEMBER(11),DECEMER(12);
private int monthCounter;
private
Months(int monthCounter) {
this.monthCounter = monthCounter;
} }
public static void
main(String[] args) {
for(Months month
: Months.values()) {
System.out.println(month +" = "+ month.monthCounter);
}
}
} |
When the constructor will be executed
once you declare the constructor inside the enum?
- When constant enum values are passed to the constructor.
- Please refer below example and try to debug it.
package simplifiedjava.crackedInterview; public class EnumConstructorMethodsDemo { public enum Months{ JANURAY(1),FEBRUARY(2),MARCH(3),APRIL(4),MAY(5),JUNE(6),JULY(7),AUGUST(8),SEPTEBER(9),OCTOBER(10),NOVEMBER(11),DECEMER(12);
private int monthCounter;
private Months(int monthCounter) {
this.monthCounter = monthCounter;
} }
public static void main(String[] args) {
for(Months month : Months.values()) {
System.out.println(month +" = "+ month.monthCounter);
}
}
} |
What is the difference between enum,
Enum and Enumeration?
- enum : enum is a keyword that can be used to define a group of named
constants.
- Enum: Enum is a class in java, present.lang package, Every Enum in
java should be a direct child class of Enum class. Hence this class is a
base class.
- Enumeration: Enumeration is an interface in java that is present in
java.util package. We can use Enumeration to get an object step by step.
package simplifiedjava.crackedInterview; public class EnumerationDemo {
public static void main(String[] args) { Vector<Integer> v = new Vector<Integer>(); v.addElement(10); v.addElement(20); v.addElement(30); v.addElement(40);
Enumeration<Integer> enu = v.elements(); while(enu.hasMoreElements()) { int element = enu.nextElement(); System.out.println(element);
}
} } |
What is Assertion? What is the role of
Assertion in java development?
- Assertion is a statement that can be used to test the assumption of your
program.
- Assertion is used for debugging purposes.
- Role of Assertion is:
1. There is a very common way
usage of System.out.println() statement for debugging but the problem of
this statement degrades the performance of the application. To overcome this
problem sun people introduced assertions.
2. The advantage of assertion is, we don’t require removing all print
statements you can simply enable or disable assertions.
package simplifiedjava.crackedInterview; import java.util.Scanner; public class AssertionExample { public static void main(String[] args) {
Scanner scanner = new
Scanner(System.in);
System.out.println("Enter Your Experience");
int exp = scanner.nextInt();
assert exp
>= 25:"Not Eligible";
System.out.println(exp);
} } output: Enter Your Experience 10 Exception in thread "main" java.lang.AssertionError: Not Eligible at simplifiedjava.crackedInterview.AssertionExample.main(AssertionExample.java:12) Make a not before running this program you have to enable assertion. Then run this program. Otherwise you wont get expected result.
|
Can you declare class inside
class?
- Without existing one type of object if there is no chance of existing
another type of object then we can declare a class inside a class.
- E.g. University can have several departments, without existing university
there is no chance of an existing departments. Hence, we have to declare
department class inside university class.
- Please refer to the below example.
package simplifiedjava.crackedInterview;
public class
University { public class Department{
}
}
|
Can you declare interface inside
interface?
- Yes, we can declare interface inside interface.
- We can take the example of Map. A map is a group of key/value
pairs.
- Each key/value pair is called Entry.
- Without an existing map object, there is no chance to exist an Entry
object. Hence, interface Entry is defined inside Map.
- Please refer to the below example.
public interface Map <K,V> { public interface Entry<K,V>{
}
} |
Can you declare interface inside
class?
- Inside a class, if we require multiple implementations of an interface and
all these implementations are related to the class then we can define an
interface inside a class.
- Please refer to the below Example.
package simplifiedjava.crackedInterview; public class InterfaceDefinedInsideClassDemo {
interface
Vehicle{
public void
getNoOfWheels(); }
class
Bus implements
Vehicle{
public void
getNoOfWheels() {
System.out.println("Six Wheels");
} }
class
Pulsor implements
Vehicle{
public void
getNoOfWheels() {
System.out.println("Two Wheeler");
} }
public static void
main(String[] args) {
InterfaceDefinedInsideClassDemo demo = new
InterfaceDefinedInsideClassDemo();
Vehicle v = demo.new
Bus();
v.getNoOfWheels();
v = demo.new
Pulsor();
v.getNoOfWheels();
}
}
Output:
Six Wheels
Two Wheeler |
Can you declare class inside
interface?
- If the functionality of the class is closely associated with the interface
then it is highly recommended to declare a class inside the interface.
- Please refer to the below example.
package simplifiedjava.crackedInterview; public interface EmailService { public void sendEmail(EmailDetails e);
class
EmailDetails{
String toList;
String ccList;
String subject;
String body;
}
} |
- 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:
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