Interview Questions and Answers - Order By Newest Q521. Will static block for Test Class execute in the following code ? class Test { static { System.out.println("Executing Static Block."); } public final int param=20; public int getParam(){ return param; } } public class Demo { public static void main(String[] args) { System.out.println(new Test().param); } }
Ans. Yes. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   static   static block   final variable   yes no Try 1 Question(s) Test Q522. Write a Program to add 1-100, 101-200, 201-300 in different threads and then sum the numbers ? Core Java
Ans. public class TotalUsingThreads extends Thread{
private static List<TotalUsingThreads> collector = new ArrayList<TotalUsingThreads>();
private int startFrom = 0;
private Integer threadTotal = null;
Test(int startFrom){
this.startFrom = startFrom;
}
public static void main(String[] args) throws InterruptedException{
int totalSum = 0;
// Create all the threads and set the count starting point for all of them
for(int count=0;count<10;count++){
TotalUsingThreads newThread = new TotalUsingThreads(count*100);
newThread.start();
collector.add(newThread);
}
boolean allCollected = false;
// Make sure that all thread totals are collected and all threads have completed their tasks
while(!allCollected){
for(int count=0;count<10;count++){
if(collector.get(count).threadTotal == null){
Thread.sleep(100);
break;
}
}
allCollected = true;
}
// sum totals of all threads
for(int count=0;count<10;count++){
totalSum += collector.get(count).threadTotal;
}
System.out.println(totalSum);
}
public void run(){
threadTotal = 0;
for(int count=startFrom;count<startFrom+100;count++){
threadTotal += count;
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  coding  code  threads  multithreading Asked in 1 Companies Q523. If you have access to a function that returns a random integer from one to five, write another function which returns a random integer from one to seven. Core Java
Ans. We can do that by pulling binary representation using 3 bits ( random(2) ).
getRandom7() {
String binaryStr = String.valuesOf(random(2))+String.valuesOf(random(2))+String.valuesOf(random(2));
binaryInt = Integer.valueOf(binaryStr);
int sumValue=0;
int multiple = 1;
while(binaryInt > 0){
binaryDigit = binaryInt%10;
binaryInt = binaryInt /10;
sumValue = sumValue + (binaryDigit * multiple);
multiple = multiple * 2;
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   tricky question   interesting questions   google interview questions   random Q524. Write a method to convert binary to a number ? Core Java
Ans.
convert(int binaryInt) {
int sumValue=0;
int multiple = 1;
while(binaryInt > 0){
binaryDigit = binaryInt%10;
binaryInt = binaryInt /10;
sumValue = sumValue + (binaryDigit * multiple);
multiple = multiple * 2;
}
return sumValue;
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   binary   interesting question   coding Q525. What will be the output of the following code ?
String s1 = "Buggy Bread";
String s2 = "Buggy Bread";
if(s1 == s2)
System.out.println("equal 1");
String n1 = new String("Buggy Bread");
String n2 = new String("Buggy Bread");
if(n1 == n2)
System.out.println("equal 2"); Core Java
Ans. equal 1 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   string class   string   string pool   code   coding basic   frequent Q526. What is Java bytecode ?
Ans. Java bytecode is the instruction set of the Java virtual machine. Each bytecode is composed by one, or two bytes that represent the instruction, along with zero or more bytes for passing parameters. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   jvm   interpreter   platform independent Asked in 1 Companies Q527. Is JVM a overhead ?
Ans. Yes and No. JVM is an extra layer that translates Byte Code into Machine Code. So Comparing to languages like C, Java provides an additional layer of translating the Source Code. C++ Compiler - Source Code --> Machine Code Java Compiler - Source Code --> Byte Code , JVM - Byte Code --> Machine Code Though it looks like an overhead but this additional translation allows Java to run Apps on all platforms as JVM provides the translation to the Machine code as per the underlying Operating System. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   jvm   interpreter   platform independent   yes-no   advanced   architecture Q528. Can we use Ordered Set for performing Binary Search ?
Ans. We need to access values on the basis of an index in Binary search which is not possible with Sets. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   sets   set   collections   binary search Q529. What is Byte Code ? Why Java's intermediary Code is called Byte Code ?
Ans. Bytecode is a highly optimized set of instructions designed to be executed by the Java run-time system. Its called Byte Code because each instruction is of 1-2 bytes. Sample instructions in Byte Code - 1: istore_1 2: iload_1 3: sipush 1000 6: if_icmpge 44 9: iconst_2 10: istore_2 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   jvm   interpreter   platform independent   byte codeAns. PATH is the variable that holds the directories for the OS to look for executables. CLASSPATH is the variable that holds the directories for JVM to look for .class files ( Byte Code ). Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   path   classpath   byte code   jvm   basic interview question Asked in 3 Companies intermediate   rare Q531. Name few access and non access Class Modifiers ?
Ans. private , public and protected are access modifiers. final and abstract are non access modifiers. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   access specifiers   access modifiers   modifiers   access control Q532. Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
Ans. LinkedHashMap Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   hashmap   linkedhashmap Q533. Is it legal to initialize List like this ? LinkedList l=new LinkedList();
Ans. No, Generic parameters cannot be primitives. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   collections   list   linkedlist   generics   yes-no Q534. What will be the output of following code ? public static void main(String[] args) { int x = 10; int y; if (x < 100) y = x / 0; if (x >= 100) y = x * 0; System.out.println("The value of y is: " + y); }
Ans. The code will not compile raising an error that the local variable y might not have been initialized. Unlike member variables, local variables are not automatically initialized to the default values for their declared type. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   default value Q535. Which markup languages can be used in restful web services ? Rest
Ans. XML and JSON ( Javascript Object Notation ). Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   web services   rest   java   j2ee   xml   json   architecture basic   frequent Q536. what will be the output of this code ?
public static void main(String[] args){
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. BuggyBread Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q537. what will be the output of this code ?
public static void main(String[] args) {
String s1=new String("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s.append("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q538. what will be the output of this code ?
public static void main(String[] args) {
StringBuffer s1=new StringBuffer("Buggy");
test(s1);
System.out.println(s1);
}
private static void test(StringBuffer s){
s=new StringBuffer("Bread");
} Core Java
Ans. Buggy Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   stringbuffer   string   method calling   pass by reference Q539. what will be the output ?
class Animal {
public void eat() throws Exception {
}
}
class Dog2 extends Animal {
public void eat(){}
public static void main(){
Animal an = new Dog2();
an.eat();
}
} Core Java
Ans. Compile Time Error: Unhandled exception type Exception Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   code   coding   overridding   late binding   exception handling   abstract class   abstract methods Q540. What are advantages of using Servlets over CGI ? Java EE
Ans. Better Performance as Servlets doesn't require a separate process for a single request. Servlets are platform independent as they are written in Java. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   j2ee   servlets   servelets   cgi Q541. What is the use of HTTPSession in relation to http protocol ? Java EE
Ans. http protocol on its own is stateless. So it helps in identifying the relationship between multiple stateless request as they come from a single source. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee   servlets   web application   session   httpsession   architecture Asked in 1 Companies Q542. Why using cookie to store session info is a better idea than just using session info in the request ? Java EE
Ans. Session info in the request can be intercepted and hence a vulnerability. Cookie can be read and write by respective domain only and make sure that right session information is being passed by the client. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee   servlets   session   session management   web applications   cookies   httpsession   architecture Q543. http protocol is by default ... ? Java EE
Ans. stateless Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  j2ee   servlets   session   session management   web applications   cookies   httpsession   architecture Q544. Can finally block throw an exception ? Core Java
Ans. Yes. Methods invoked from within a finally block can throw an exception. Failure to catch and handle such exceptions results in the abrupt termination of the entire try block. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   exceptions   exception handling   finally   yes-no Q545. Which of the following is a canonical path ? 1. C:\directory\..\directory\file.txt 2. C:\directory\subDirectory1\directory\file.txt 3. \directory\file.txt
Ans. 2nd Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   file   fileio Q546. What will the following code print when executed on Windows ?
public static void main(String[] args){
String parent = null;
File file = new File("/file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
} Core Java
Ans. file.txtC:file.txtC:file.txt Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   file   fileio   coding   code   file handling Q547. What will be the output of following code ?
public static void main(String[] args){
String child = null;
File file = new File("/folder", child);
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
} Core Java
Ans. NullPointerException at line:File file = new File("/folder", child); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   file   fileio   coding   code   file handling Q548. What will be the output of following code, assuming that currently we are in c:Project ?
public static void main(String[] args){
String child = null;
File file = new File("../file.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
try {
System.out.println(file.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} Core Java
Ans. ..file.txt
C:WorkspaceProject..file.txt
C:Workspacefile.txt Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   file   fileio   coding   code   file handling Q549. Which is the abstract parent class of FileWriter ? Core Java
Ans. OutputStreamWriter Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   file   fileio   filewriter   outputstreamwriter  file handling Q550. Which class is used to read streams of characters from a file? Core Java
Ans. FileReader Sample Code for FileReader Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  java   io   file   fileio   filereader  file handling