SlideShare a Scribd company logo
1 of 6
Basic Java Interview Questions And Answers
1. Why is the main method static ?
Ans: So that it can be invoked without creating an instance of that class
2. What is the difference between class variable, member variable and
automatic(local) variable ?
Ans: - class variable is a static variable and does not belong to instance of class but
rather shared across all the instances
- member variable belongs to a particular instance of class and can be called from any
method of the class
- automatic or local variable is created on entry to a method and has only method
scope
3. When are static and non static variables of the class initialized ?
Ans: The static variables are initialized when the class is loaded Non static variables
are initialized just before the constructor is called
4. When are automatic variable initialized ?
Ans: Automatic variable have to be initialized explicitly
5. What is a modulo operator % ?
Ans: This operator gives the value which is related to the remainder of a divisione.g
x=7%4 gives remainder 3 as an answer
6. How is an argument passed in java, by copy or by reference What is a modulo
operator % ?
Ans: This operator gives the value which is related to the remainder of a divisione.g
x=7%4 gives remainder 3 as an answer
7. What is garbage collection ?
Ans: The runtime system keeps track of the memory that is allocated and is able to
determine whether that memory is still useable. This work is usually done in
background by a low-priority thread that is referred to as garbage collector. When the
gc finds memory that is no longer accessible from any live thread it takes steps to
release it back to the heap for reuse
8. Does System.gc and Runtime.gc() guarantee garbage collection ?
Ans: No
9. What are different types of operators in Java ?
Ans: - Uniary ++, –, +, -, |, ~, ()
- Arithmetic *, /, %,+, -Shift <<, >>, >>>
- Comparison =, instanceof, = =,!=Bitwise &, ^, |Short Circuit &&, ||
Ternary ?:Assignment =

www.javaelearn.com

training@javaelearn.com

+1-732-546-3607
Basic Java Interview Questions And Answers
10. How does bitwise (~) operator work ?
Ans: It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, e.g
11110000 coverts to 00001111
11. Can shift operators be applied to float types ?
Ans: No, shift operators can be applied only to integer or long types
12. What happens to the bits that fall off after shifting ?
Ans: They are discarded
13. What values of the bits are shifted in after the shift ?
Ans: In case of signed left shift >> the new bits are set to zero. But in case of signed
right shift it takes the value of most significant bit before the shift, that is if the most
significant bit before shift is 0 it will introduce 0, else if it is 1, it will introduce 1
14. What are access modifiers ?
Ans: These public, protected and private, these can be applied to class, variables,
constructors and methods. But if you don�t specify an access modifier then it is
considered as Friendly
15. Can protected or friendly features be accessed from different packages ?
Ans: No when features are friendly or protected they can be accessed from all the
classes in that package but not from classes in another package
16. How can you access protected features from another package ?
Ans: You can access protected features from other classes by subclassing the that
class in another package, but this cannot be done for friendly features
17. What are the rules for overriding ?
Ans: Private method can be overridden by private, friendly, protected or public
methods. Friendly method can be overridden by friendly, protected or public methods.
Protected method can be overridden by protected or public methods. Public method
can be overridden by public method
18. Can you change the reference of the final object ?
Ans: No the reference cannot be change, but the data in that object can be changed
19. Can abstract modifier be applied to a variable ?
Ans: No it is applied only to class and methods
20. Can abstract class be instantiated ?
Ans: No abstract class cannot be instantiated i.e you cannot create a new object of this
class

www.javaelearn.com

training@javaelearn.com

+1-732-546-3607
Basic Java Interview Questions And Answers
21. When does the compiler insist that the class must be abstract ?
Ans: If one or more methods of the class are abstract. If class inherits one or more
abstract methods from the parent abstract class and no implementation is provided for
that method If class implements an interface and provides no implementation for
those methods
22. How is abstract class different from final class ?
Ans: Abstract class must be subclassed and final class cannot be subclassed
23. Where can static modifiers be used ?
Ans: They can be applied to variables, methods and even a block of code, static
methods and variables are not associated with any instance of class
24. When are the static variables loaded into the memory ?
Ans: During the class load time
25. When are the non static variables loaded into the memory ?
Ans: They are loaded just before the constructor is called
26. How can you reference static variables ?
Ans: Via reference to any instance of the class
Code:
Computer comp = new Computer ();
comp.harddisk where hardisk is a static variable
comp.compute() where compute is a method
Via the class name
Code:
Computer.harddisk
Computer.compute()
27. Can static method use non static features of there class ?
Ans: No they are not allowed to use non static features of the class, they can only call
static methods and can use static data
28. What is static initializer code ?
Ans: A class can have a block of initializer code that is simply surrounded by curly
braces and labeled as static e.g.
Code:
public class Demo{
static int =10;
static{
System.out.println(�Hello world�);
}
}

www.javaelearn.com

training@javaelearn.com

+1-732-546-3607
Basic Java Interview Questions And Answers
And this code is executed exactly once at the time of class load
29. Where is native modifier used ?
Ans: It can refer only to methods and it indicates that the body of the method is to be
found else where and it is usually written in non java language
30. What are transient variables ?
Ans: A transient variable is not stored as part of objects persistent state and they
cannot be final or static
31. What is synchronized modifier used for ?
Ans: It is used to control access of critical code in multithreaded programs
32. What are volatile variables ?
Ans: It indicates that these variables can be modified asynchronously
33. What are the rules for primitive arithmetic promotion conversion ?
Ans: For Unary operators :
If operant is byte, short or a char it is converted to an int. If it is any other type it is not
converted
For binary operands :
If one of the operands is double, the other operand is converted to double
Else If one of the operands is float, the other operand is converted to float
Else If one of the operands is long, the other operand is converted to long
Else both the operands are converted to int
34. What are the rules for casting primitive types ?
Ans: You can cast any non Boolean type to any other non boolean type. You cannot
cast a boolean to any other type; you cannot cast any other type to a boolean
35. What are the rules for object reference assignment and method call conversion ?
Ans: An interface type can only be converted to an interface type or to object. If the
new type is an interface, it must be a superinterface of the old type.
A class type can be converted to a class type or to an interface type. If converting to a
class type the new type should be superclass of the old type. If converting to an
interface type new type the old class must implement the interface.
An array maybe converted to class object, to the interface cloneable, or to an array.
Only an array of object references types may be converted to an array, and the old
element type must be convertible to the new element
36. What are the rules for Object reference casting ?
Ans: Casting from Old types to Newtypes
Compile time rules :
- When both Oldtypes and Newtypes are classes, one should be subclass of the other

www.javaelearn.com

training@javaelearn.com

+1-732-546-3607
Basic Java Interview Questions And Answers
- When both Oldtype ad Newtype are arrays, both arrays must contain reference types
(not primitive), and it must be legal to cast an element of Oldtype to an element of
Newtype
- You can always cast between an interface and a non-final object
Runtime rules :
- If Newtype is a class. The class of the expression being converted must be Newtype
or must inherit from Newtype
- If NewType is an interface, the class of the expression being converted must
implement Newtype
37. When do you use continue and when do you use break statements ?
Ans: When continue statement is applied it prematurely completes the iteration of a
loop. When break statement is applied it causes the entire loop to be abandoned.
38. What is the base class from which all exceptions are subclasses ?
Ans: All exceptions are subclasses of a class called java.lang.Throwable
39. How do you intercept and thereby control exceptions ?
Ans: We can do this by using try/catch/finally blocks
You place the normal processing code in try block
You put the code to deal with exceptions that might arise in try block in catch block
Code that must be executed no matter what happens must be place in finally block
40. When do we say an exception is handled ?
Ans: When an exception is thrown in a try block and is caught by a matching catch
block, the exception is considered to have been handled
41. When do we say an exception is not handled ?
Ans: There is no catch block that names either the class of exception that has been
thrown or a class of exception that is a parent class of the one that has been thrown,
then the exception is considered to be unhandled, in such condition the execution
leaves the method directly as if no try has been executed
42. In what sequence does the finally block gets executed ?
Ans: If you put finally after a try block without a matching catch block then it will be
executed after the try block
If it is placed after the catch block and there is no exception then also it will be
executed after the try block
If there is an exception and it is handled by the catch block then it will be executed
after the catch block
43. What can prevent the execution of the code in finally block ?
Ans: - The death of thread
- Use of system.exit()

www.javaelearn.com

training@javaelearn.com

+1-732-546-3607
Basic Java Interview Questions And Answers
- Turning off the power to CPU
- An exception arising in the finally block itself
What are the rules for catching multiple exceptions
- A more specific catch block must precede a more general one in the source, else it
gives compilation error
- Only one catch block, that is first applicable one, will be executed
44. What does throws statement declaration in a method indicate ?
Ans: This indicates that the method throws some exception and the caller method
should take care of handling it
45. What are checked exception ?
Ans: Checked exceptions are exceptions that arise in a correct program, typically due
to user mistakes like entering wrong data or I/O problems
46. What are runtime exceptions ?
Ans: Runtime exceptions are due to programming bugs like out of bond arrays or null
pointer exceptions.
47. What is difference between Exception and errors ?
Ans: Errors are usually compile time and exceptions can be runtime or checked
48. How will you handle the checked exceptions ?
Ans: You can provide a try/catch block to handle it. OR
Make sure method declaration includes a throws clause that informs the calling
method an exception might be thrown from this particular method
When you extend a class and override a method, can this new method throw
exceptions other than those that were declared by the original method
No it cannot throw, except for the subclasses of those exceptions
49. Is it legal for the extending class which overrides a method which throws an
exception, not o throw in the overridden class ?
Ans: Yes it is perfectly legal
50. Explain modifier final ?
Ans: Final can be applied to classes, methods and variables and the features cannot be
changed. Final class cannot be subclassed, methods cannot be overridden.

www.javaelearn.com

training@javaelearn.com

+1-732-546-3607

More Related Content

Recently uploaded

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 

Basic java interview question and answers

  • 1. Basic Java Interview Questions And Answers 1. Why is the main method static ? Ans: So that it can be invoked without creating an instance of that class 2. What is the difference between class variable, member variable and automatic(local) variable ? Ans: - class variable is a static variable and does not belong to instance of class but rather shared across all the instances - member variable belongs to a particular instance of class and can be called from any method of the class - automatic or local variable is created on entry to a method and has only method scope 3. When are static and non static variables of the class initialized ? Ans: The static variables are initialized when the class is loaded Non static variables are initialized just before the constructor is called 4. When are automatic variable initialized ? Ans: Automatic variable have to be initialized explicitly 5. What is a modulo operator % ? Ans: This operator gives the value which is related to the remainder of a divisione.g x=7%4 gives remainder 3 as an answer 6. How is an argument passed in java, by copy or by reference What is a modulo operator % ? Ans: This operator gives the value which is related to the remainder of a divisione.g x=7%4 gives remainder 3 as an answer 7. What is garbage collection ? Ans: The runtime system keeps track of the memory that is allocated and is able to determine whether that memory is still useable. This work is usually done in background by a low-priority thread that is referred to as garbage collector. When the gc finds memory that is no longer accessible from any live thread it takes steps to release it back to the heap for reuse 8. Does System.gc and Runtime.gc() guarantee garbage collection ? Ans: No 9. What are different types of operators in Java ? Ans: - Uniary ++, –, +, -, |, ~, () - Arithmetic *, /, %,+, -Shift <<, >>, >>> - Comparison =, instanceof, = =,!=Bitwise &, ^, |Short Circuit &&, || Ternary ?:Assignment = www.javaelearn.com training@javaelearn.com +1-732-546-3607
  • 2. Basic Java Interview Questions And Answers 10. How does bitwise (~) operator work ? Ans: It converts all the 1 bits in a binary value to 0s and all the 0 bits to 1s, e.g 11110000 coverts to 00001111 11. Can shift operators be applied to float types ? Ans: No, shift operators can be applied only to integer or long types 12. What happens to the bits that fall off after shifting ? Ans: They are discarded 13. What values of the bits are shifted in after the shift ? Ans: In case of signed left shift >> the new bits are set to zero. But in case of signed right shift it takes the value of most significant bit before the shift, that is if the most significant bit before shift is 0 it will introduce 0, else if it is 1, it will introduce 1 14. What are access modifiers ? Ans: These public, protected and private, these can be applied to class, variables, constructors and methods. But if you don�t specify an access modifier then it is considered as Friendly 15. Can protected or friendly features be accessed from different packages ? Ans: No when features are friendly or protected they can be accessed from all the classes in that package but not from classes in another package 16. How can you access protected features from another package ? Ans: You can access protected features from other classes by subclassing the that class in another package, but this cannot be done for friendly features 17. What are the rules for overriding ? Ans: Private method can be overridden by private, friendly, protected or public methods. Friendly method can be overridden by friendly, protected or public methods. Protected method can be overridden by protected or public methods. Public method can be overridden by public method 18. Can you change the reference of the final object ? Ans: No the reference cannot be change, but the data in that object can be changed 19. Can abstract modifier be applied to a variable ? Ans: No it is applied only to class and methods 20. Can abstract class be instantiated ? Ans: No abstract class cannot be instantiated i.e you cannot create a new object of this class www.javaelearn.com training@javaelearn.com +1-732-546-3607
  • 3. Basic Java Interview Questions And Answers 21. When does the compiler insist that the class must be abstract ? Ans: If one or more methods of the class are abstract. If class inherits one or more abstract methods from the parent abstract class and no implementation is provided for that method If class implements an interface and provides no implementation for those methods 22. How is abstract class different from final class ? Ans: Abstract class must be subclassed and final class cannot be subclassed 23. Where can static modifiers be used ? Ans: They can be applied to variables, methods and even a block of code, static methods and variables are not associated with any instance of class 24. When are the static variables loaded into the memory ? Ans: During the class load time 25. When are the non static variables loaded into the memory ? Ans: They are loaded just before the constructor is called 26. How can you reference static variables ? Ans: Via reference to any instance of the class Code: Computer comp = new Computer (); comp.harddisk where hardisk is a static variable comp.compute() where compute is a method Via the class name Code: Computer.harddisk Computer.compute() 27. Can static method use non static features of there class ? Ans: No they are not allowed to use non static features of the class, they can only call static methods and can use static data 28. What is static initializer code ? Ans: A class can have a block of initializer code that is simply surrounded by curly braces and labeled as static e.g. Code: public class Demo{ static int =10; static{ System.out.println(�Hello world�); } } www.javaelearn.com training@javaelearn.com +1-732-546-3607
  • 4. Basic Java Interview Questions And Answers And this code is executed exactly once at the time of class load 29. Where is native modifier used ? Ans: It can refer only to methods and it indicates that the body of the method is to be found else where and it is usually written in non java language 30. What are transient variables ? Ans: A transient variable is not stored as part of objects persistent state and they cannot be final or static 31. What is synchronized modifier used for ? Ans: It is used to control access of critical code in multithreaded programs 32. What are volatile variables ? Ans: It indicates that these variables can be modified asynchronously 33. What are the rules for primitive arithmetic promotion conversion ? Ans: For Unary operators : If operant is byte, short or a char it is converted to an int. If it is any other type it is not converted For binary operands : If one of the operands is double, the other operand is converted to double Else If one of the operands is float, the other operand is converted to float Else If one of the operands is long, the other operand is converted to long Else both the operands are converted to int 34. What are the rules for casting primitive types ? Ans: You can cast any non Boolean type to any other non boolean type. You cannot cast a boolean to any other type; you cannot cast any other type to a boolean 35. What are the rules for object reference assignment and method call conversion ? Ans: An interface type can only be converted to an interface type or to object. If the new type is an interface, it must be a superinterface of the old type. A class type can be converted to a class type or to an interface type. If converting to a class type the new type should be superclass of the old type. If converting to an interface type new type the old class must implement the interface. An array maybe converted to class object, to the interface cloneable, or to an array. Only an array of object references types may be converted to an array, and the old element type must be convertible to the new element 36. What are the rules for Object reference casting ? Ans: Casting from Old types to Newtypes Compile time rules : - When both Oldtypes and Newtypes are classes, one should be subclass of the other www.javaelearn.com training@javaelearn.com +1-732-546-3607
  • 5. Basic Java Interview Questions And Answers - When both Oldtype ad Newtype are arrays, both arrays must contain reference types (not primitive), and it must be legal to cast an element of Oldtype to an element of Newtype - You can always cast between an interface and a non-final object Runtime rules : - If Newtype is a class. The class of the expression being converted must be Newtype or must inherit from Newtype - If NewType is an interface, the class of the expression being converted must implement Newtype 37. When do you use continue and when do you use break statements ? Ans: When continue statement is applied it prematurely completes the iteration of a loop. When break statement is applied it causes the entire loop to be abandoned. 38. What is the base class from which all exceptions are subclasses ? Ans: All exceptions are subclasses of a class called java.lang.Throwable 39. How do you intercept and thereby control exceptions ? Ans: We can do this by using try/catch/finally blocks You place the normal processing code in try block You put the code to deal with exceptions that might arise in try block in catch block Code that must be executed no matter what happens must be place in finally block 40. When do we say an exception is handled ? Ans: When an exception is thrown in a try block and is caught by a matching catch block, the exception is considered to have been handled 41. When do we say an exception is not handled ? Ans: There is no catch block that names either the class of exception that has been thrown or a class of exception that is a parent class of the one that has been thrown, then the exception is considered to be unhandled, in such condition the execution leaves the method directly as if no try has been executed 42. In what sequence does the finally block gets executed ? Ans: If you put finally after a try block without a matching catch block then it will be executed after the try block If it is placed after the catch block and there is no exception then also it will be executed after the try block If there is an exception and it is handled by the catch block then it will be executed after the catch block 43. What can prevent the execution of the code in finally block ? Ans: - The death of thread - Use of system.exit() www.javaelearn.com training@javaelearn.com +1-732-546-3607
  • 6. Basic Java Interview Questions And Answers - Turning off the power to CPU - An exception arising in the finally block itself What are the rules for catching multiple exceptions - A more specific catch block must precede a more general one in the source, else it gives compilation error - Only one catch block, that is first applicable one, will be executed 44. What does throws statement declaration in a method indicate ? Ans: This indicates that the method throws some exception and the caller method should take care of handling it 45. What are checked exception ? Ans: Checked exceptions are exceptions that arise in a correct program, typically due to user mistakes like entering wrong data or I/O problems 46. What are runtime exceptions ? Ans: Runtime exceptions are due to programming bugs like out of bond arrays or null pointer exceptions. 47. What is difference between Exception and errors ? Ans: Errors are usually compile time and exceptions can be runtime or checked 48. How will you handle the checked exceptions ? Ans: You can provide a try/catch block to handle it. OR Make sure method declaration includes a throws clause that informs the calling method an exception might be thrown from this particular method When you extend a class and override a method, can this new method throw exceptions other than those that were declared by the original method No it cannot throw, except for the subclasses of those exceptions 49. Is it legal for the extending class which overrides a method which throws an exception, not o throw in the overridden class ? Ans: Yes it is perfectly legal 50. Explain modifier final ? Ans: Final can be applied to classes, methods and variables and the features cannot be changed. Final class cannot be subclassed, methods cannot be overridden. www.javaelearn.com training@javaelearn.com +1-732-546-3607