10 of the trickiest Java Interview Questions PART -2 - CODER

Artikel Terbaru

test banner

Post Top Ad

Responsive Ads Here

10 of the trickiest Java Interview Questions PART -2

Share This





1. How can you do constructor chaining in Java?
Ans: Constructor chaining is the process of calling one constructor from another constructor with respect to current object.
Constructor chaining can be done in two ways:
§  Within same class: It can be done using this() keyword for constructors in same class
§  From base class: by using super() keyword to call constructor from the base class.


2. Anything related to Thread Pooling?
Ans : “Nearly half (or maybe more) aren’t even aware that Java comes with a Thread Pool. Some of them don’t even know what a Thread Pool is. Most people who do know… are unable to explain why you would use a Thread Pool over simply starting threads on demand.
“This really points to a systemic problem among Java developers. They are too removed from the underlying hardware. Because the language isolates you from the OS doesn’t mean that you stop thinking about how the underlying systems will react to your software. This is especially true when the language gives you access to OS level resources (like Threads, or Files or memory).
“You can’t be starting and stopping thousands of threads, or having millions of files open or having terabytes of memory allocated. Java can’t create resources. It’s not magic.

3. How can we find the memory usage of JVM from Java code?
Ans: 1) to get the maximum amount of memory that your Java application can use: 
Runtime runtime = Runtime.getRuntime();
System.out.println("max memory: " + runtime.maxMemory() / 1024);

2)to get how much memory that JVM has allocated for your application
Runtime runtime = Runtime.getRuntime();
System.out.println("allocated memory: " + runtime.totalMemory() / 1024);

3)to get how much memory is being used by your application:
Runtime runtime = Runtime.getRuntime();
System.out.println("free memory: " + runtime.freeMemory() / 1024); 

4. Can you catch an exception thrown by another thread in Java?
Ans: You can add the definition of that thread in try-catch block.
or you can put the function call in different try-catch block like:
try{
   t1.sleep();
   }catch(InterruptedException e){}

try{
    t2.start();
    }catch(IllegalThreadStateException e){}

5. Does this code compile?
Ans : Does this code compile? If so, when executed, what is the output? Just look at it. Don’t compile it, place it in IDE, or run it—figure it out yourself. The answer is obvious, if not immediately apparent.”
java interview questions

6. How can you check if a String is a number by using regular expression?
Ans: Using regular expressions is costly in terms of performance. Trying to parse string as a long value is inefficient and unreliable, and may be not what you need.
What I suggest is to simply check if each character is a digit, what can be efficiently done using Java 8 lambda expressions:
boolean isNumeric = someString.chars().allMatch(x -> Character.isDigit(x));

7. What is the difference between abstract class and interface.
Ans : Abstract class can have zero or more implemented methods, and interface cannot have any implemented methods.
Is this the only difference? …Do not stop here. Keep answering by listing out other differences as well. [For example] one of them could be, members of interface cannot have any access level other than public. 
[Another is that] variables in interface are not actually variables; they are consants, because they are public static final by default.”

8. Is an empty .java file name a valid source file name?
Yes it is,You save your java file by .java only, compile it by javac .java and run by java yourclassname.
lets take an example!
1. //save by .java only
2. class A{
3. public static void main(String args[]){
4. System.out.println("Hello java");
5. }
6. }
7. //compile by javac .java
8. //run by java A
Compile it by javac .javaRun it by java A
Compile & run successfully.
Output: Hello java


9. Tricky output questions
A guy submitted one of the tricky output-related Java interview question. “Many would say that it’s NullPointerException,” But it’s not. It will print ‘hello’.
java interview questions

10. Beware of over-complicating things
       Analyse the code properly!!
java interview questions

“The code supposedly starts a thread which is counting on a long variable, then after 1 second, another thread will set the counting thread’s loop condition to false. The correct behaviour should be the thread printing the value to output.
“However, it loops forever. The counting thread doesn’t see the value of the stop variable’s new assignment… How can it be fixed ?
“The solution: stop class variable should be declared as volatile.”
.



No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here

Pages