More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. An exception is an unwanted or unexpected event, which occurs during the execution of a program that is at run time, that disrupts the normal flow of the program?s instructions. these are different types arithmetic exceptions, class not found exception, interrupted exception, run time exception etc.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Class lengofstring {
Public static void main(String[] arr) {
int i = 0;
String s = "hello world!";
for (char C: s.toCharArray()) i;
System.out.println("Length: "
i);
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. import java.util.Scanner;
public class Prime
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter n to compute the nth prime number: ");
int nth = sc.nextInt();
int num, count, i;
num=1;
count=0;
while (count < nth){
num=num 1;
for (i = 2; i <= num; i )
{
if (num % i == 0)
{
break;
}
}
if ( i == num)
{
count = count 1;
}
}
System.out.println("Value of nth prime: " num);
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. int main ()
{
int a []={1..n};
int miss=getMiss (a,n);
Printf ("%d",miss);
}
getMiss (int a [],int n){
int I,total;
total=(n 1)*(n 2);
for (i = 0; i < n; i++)
total -= a[i];
return total;
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. Yes , but only the read performance in case we don't need to modify it much. In case we need to modify it a lot , it creates write performance overheads as we would need to create many news objects instead of just changing one.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Immutable objects relieves us from the problems of inconsistencies and security and helps with better read performance but at the same time are write performance and storage overheads. In case any modification is required , a new object is created and thus creating multiple copies of it.
This is the reason we use StringBuffer / StringBuilder when we have to append some text multiple times and then create a String out of it.
Help us improve. Please let us know the company, where you were asked this question :
Ans. // Java code for serialization and deserialization
// of a Java object
import java.io.*;
class Demo implements java.io.Serializable
{
public int a;
public String b;
// Default constructor
public Demo(int a, String b)
{
this.a = a;
this.b = b;
}
}
class Test
{
public static void main(String[] args)
{
Demo object = new Demo(1, "geeksforgeeks");
String filename = "file.ser";
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(object);
out.close();
file.close();
System.out.println("Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
Demo object1 = null;
// Deserialization
try
{
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
object1 = (Demo)in.readObject();
in.close();
file.close();
System.out.println("Object has been deserialized ");
System.out.println("a = " object1.a);
System.out.println("b = " object1.b);
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
catch(ClassNotFoundException ex)
{
System.out.println("ClassNotFoundException is caught");
}
}
}
Help us improve. Please let us know the company, where you were asked this question :
Ans. A program that uses HTTP for serving files that create web pages for users in response to their requests that are sent by the HTTP clients of their computer is called as a web server.
Web - Server Types
Apache HTTP Server by the Apache Software Foundation
Internet Information Service from Microsoft
Sun Java System Web Server
Jigsaw Server.
Help us improve. Please let us know the company, where you were asked this question :
Ans. @Import annotation in Spring allows you to load bean definitions from one or more another @Configuration files or Components. You might not want to configure all the beans in one configuration file.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Second level cache is shared between sessions. Hibernate by default uses first level cache. Second level cache need to be configurd to use.
There are several second level cache e.g eh cache,jboss cache etc.
Help us improve. Please let us know the company, where you were asked this question :
Q892. Why do we need to specify import statement ? Don't you think Java could have been designed in such a way to automatically import everything that's in the class path ?
Ans. Java could design it in such a manner but would result in ambiguity if there are multiple files with the same name. The only way to get over it to show the message when you use any such class to provide explicit import with the package prefix. The other problem could be that Java might have to change the early import to Late import and check what's being used to decide what needs to be imported as otherwise you will see errors regarding the ambiguous imports with duplicate file name.
Help us improve. Please let us know the company, where you were asked this question :
Ans. No,we cannot.I t will give concurrentModificationExceptin error. It can be resolved by using ConcurrentClasses like ConcurrentHashMap,CopyOnWriteArrayList,BlockingQueue etc which are fail-safe and wont give exception.
Help us improve. Please let us know the company, where you were asked this question :