| Back to Questions Page |
| |
| Question |
Sample code to retrieve objects from HashMap in sorted
ascending order? |
Rank |
Answer Posted By |
|
Question Submitted By :: Prakash.rajendra |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | Map hm = new HashMap();
//add values to hashmap
Map tm = new TreeMap(hm);
//Iterate keySet in tm  |
| Murli |
| |
| |
| Question |
what is real-time example of runtime polymorphism and
compile time polymorphism |
Rank |
Answer Posted By |
|
Question Submitted By :: Swapna.gorentla |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | Run time polymorphism: Bank Account, savings and current
(here, acoount class is the base class, savings and current
account are derived classes, the objects of each are created
at runtime)
Compile time polymorphism: calculating area of square and
rectangle (different classes, square and rectangle have a
function called area() which takes different arguments (for
square only one argument and for rectangle two arguments-) )  |
| Rushugroup |
| |
| |
| Question |
I have one POJO class(Java bean class), it has two
variables for that it has setters and getters. Now i have
created two objects for that class and i have set the data
for those variables through this two objects. Now question
is i want check whether those two objects have same data or
not, for this write a program? Thanks, Bose. |
Rank |
Answer Posted By |
|
Question Submitted By :: Bose |
| This Interview Question Asked @ Oracle |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | //This is Pojo class with two property name and email
class PojoBean {
private String name;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
// main class to test pojo object
class PojoMain
{
public static void main(String[] args)
{
//create first instance of the Pojo Class and set property
values.
PojoBean pBeanOne = new PojoBean();
pBeanOne.setName("Pushpa");
pBeanOne.setEmail("ashish@gmail.com");
//create second instance of the Pojo Class and set
property values.
PojoBean pBeanTwo = new PojoBean();
pBeanTwo.setName("ashish");
pBeanTwo.setEmail("ashish@gmail.com");
//code to check two object is having same data or not.
if(pBeanOne.getName().equals(pBeanTwo.getName())){
System.out.println("Two object is having same Name");
}
// second property email
if(pBeanOne.getEmail().equals(pBeanTwo.getEmail())){
System.out.println("Two object is having same email");
}
}
}
 |
| Pushpa |
| |
| |
|
|
| |
| Answer | override equals method from object class and have the comparison logic inside that.Also override hashcode method..
Now pBeanOne.equals(pBeanTwo) will give you the result  |
| Murli |
| |
| |
| Question |
Read data from console and print in one file. That would be
in C:\temp ? Thanks, Bose |
Rank |
Answer Posted By |
|
Question Submitted By :: Bose |
| This Interview Question Asked @ Oracle , Surayasoftware |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | //Read data from console and print in one file.
import java.io.*;
class ReadDataFromConsole
{
public static void main(String[] args)
{
try{
String fileStr = ""; // declare a string variable
for(int i = 0; i<args.length; i++){ // for loop start
with argument size
fileStr = fileStr + " " + args[i];
}
//create an instance of the random acces file "rfile'.
RandomAccessFile rfile = new
RandomAccessFile("C:/Temp/text.txt","rw");
// write object into the file.
rfile.writeBytes(fileStr);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
 |
| Pushpa |
| |
| |
| Question |
what is diffrence between .NET and java? |
Rank |
Answer Posted By |
|
Question Submitted By :: Guest |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | it is clear that .netis platform independent because it
cannot work on every operating system...and also database
connectivityis not facilated by .net but in case of
java ..it fullfills the the above two problem  |
| Subhransu Sekhar Das |
| |
| |
| Answer | .net is related to only microsoft.It can be run in
microsoft only.It is platform dependent.whereas java is
platform independent i.e.,It can run on any OS  |
| Veeramani |
| |
| |
| Question |
Create a form of user Login in JSP, that accepts the user
name and password from user and authenticate it with user
names and passwords stored in database.
(Plz provide me answer immediately) |
Rank |
Answer Posted By |
|
Question Submitted By :: Vipingupta |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | first you should to create a jsp
like login.jsp
<%@page language="java"%>
<html>
<head>
<title>fill form</title>
<body>
<form action ="loginservlet.java" mrthod=post>
<input type =text ,name=username ,value="">
<imput type =password,name =password,value="">
</form>
</body>
....
loinservlet.java
import java.io.*;
import javax.servlet.*;
import java.sql.*;
class Loginservlet extends HttpSevlet
{
public void dopost(HttpServletRequest request,
HttpServletResponse response)throws
ServletException,IOException
{
String connectionURL
= "jdbc:mysql://192.168.10.59/datbasename";
Connection connection=null;
ResultSet rs;
String userName=new String("");
String passwrd=new String("");
response.setContentType("text/html");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection
(connectionURL, "root", "root");
String sql = "select user,password from User";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
username=rs.getString("user");
passwrd=rs.getString("password");
}
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
if(userName.equals(request.getParameter("usernmae"))
&&
passwrd.equals(request.getParameter
("password"))){
out.println("User Authenticated");
}
else{
out.println("You are not an authentic person");
}
}
}
bydefault the sql run on above port but if you changed it
so please chkit and give the port nuber which you customize
Class.forname(): we pass the string of particular driver
and it automaticaly convert it inot Class object
thanks AMIT SINGH09
 |
| Amit2009mca |
| |
| |
| Question |
"We cannot create an object of interface but we can create a
variable of it". Discuss the statement with the help of an
example.
(Plz help us to provide immediately.) |
Rank |
Answer Posted By |
|
Question Submitted By :: Vipingupta |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | In layman terms it can be said that a interface basicaly
provides a template. Since it does not have any concreet
functions we cannot create its object, as during
implemetation(runtime) there would be no definition found
for the particular function that would have been hardcoded
into the program. Where as using it as a refernce variable
is possible because of the technique provided by java
i.e. 'Virtual Method Invocation'. This enables us to create
a variable of a interface, but at runtime the function
defenition that would be called would be the one of the
object of the class actually being referred. It would get
clear from the following example:
interface Color
{
void green();
void red();
void blue();
}
here if we create a object of interface Color like
Color clr= new Color();
clr.green();
then it would give an error as there is no implementation
available for the function green();
whereas this senario is valid where we have another class
class Implemeter
{
public void green()
{
System.out.println("You selected green color");
}
public void red()
{
System.out.println("You selected red color");
}
}
and now we use like this
Color clr = new Implementer();
clr.green();
then this is absolutely valid as on runtime the function
definition of green() from class Implementer would be
called and not of interface Color. This technique is called
virtual method invocation.
Just let me know if this information was helpful, or if you
have any other doubts.  |
| Nick |
| |
| |
| Answer | ur program defintly right but i cannot understand theory
write by u.  |
| Simran |
| |
| |
| Question |
Write program to print Hello World and print each character
address in that string and print how many times each
character is in that string?
Ex: H: 0 & 1
e:1 & 1
l :2,3,8 & 3
o:4,6 & 2
w:5 & 1
r: 7 & 1
d 9 & 1 |
Rank |
Answer Posted By |
|
Question Submitted By :: Seenu |
| This Interview Question Asked @ Huawei , Ibm |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | class Wordcount
{
public static void main(String[] args)
{
String s="hello world";
for(int i=0;i<s.length();i++)
{
String s1=""+i;
int count=1;
if(s.charAt(i)!=' ')
{
for(int j=i+1;j<s.length();j++)
{
if(s.charAt(i)==s.charAt(j))
{
count++;
s1=s1+"&"+j;
}
}
int before=0;
for(int x=i-1;x>0;x--)
{
if(s.charAt(i)==s.charAt(x))
before=1;
}
if(before==0)
System.out.println("the occurence of letter "+s.charAt(i)+" no of times= "+count+"--- positions="+s1);
}
}
}
}  |
| Kishore Nerella |
| |
| |
| Answer | Little change in the code
add this for loop
for (int x = i - 1; x >= 0; x--)
instead of
for (int x = i - 1; x > 0; x--)  |
| Noor |
| |
| |
| Answer | public class WordCount {
public static void main(String args[])
{
String s = "HelloWorld";
for(int i=0; i<s.length(); i++) //Track
each character
{
int flag = 0, count = 0;
for(int j=i-1; j>=0; j--)
//This loop is for: If character repeats or Space
then skip to next character
{
if(s.charAt(j) == s.charAt
(i) || s.charAt(i) == ' ')
{
flag = 1;
break;
}
}
if(flag == 1)
continue;
System.out.print(s.charAt(i)
+ ": "); //Starts to check position and counter for
repeated characters
for(int k=i; k<s.length(); k++)
{
if(s.charAt(i) == s.charAt
(k))
{
count++;
if(k ==
i) //Just for nice output
System.out.print(k);
else
System.out.print("," + k);
}
}
System.out.println(" & " +
count); //end output line with count
}
}
}  |
| Nagvthu@gmail.com |
| |
| |
| Question |
JSP is by default thread safe or not? what is the meaning of
isThreadSafe="true" and isThreadSafe="false". Explain it?
Thanks,
Seenu |
Rank |
Answer Posted By |
|
Question Submitted By :: Seenu |
| This Interview Question Asked @ Huawei |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | Accroding to java documention, default value for jsp page
attribute 'isThreadsafe', is set to true, which means jsp
can handle multiple client requests and does not implement
SingleThreadModel interface.
Well, thinking gramatically, meaning of 'isThreadsafe' is to
clarify or to assert whether jsp is threadsafe or not.
So it makes sense when you put it's value to true ie.
isThreadsafe = true, meaning jsp is now thread safe and so
should handle one request at a time ie. it should implement
SingleThreadModel interface.
And isThreadsafe = false implies, jsp is not thread safe and
so it can handle multiple client requests.  |
| Nehru |
| |
| |
| Answer | jsp is not thread safe.
if threadsafe="true" it means only one thread can access an
object at a time.
if threadsafe="false" it means multiple threads can access
one object at a time concurrently.  |
| Vikas |
| |
| |
| Answer | JSP is by default its not a thread safe suppose we prevent
the JSP page from thread safe then
if threadsafe="true" it means only one thread can access an
object at a time.it handle only client request at time.it
should implements singleThreaded model interface.
isThreadsafe = false implies, jsp is not thread safe and
so it can handle multiple client requests.i.e means number
of clients access the only object  |
| Srinu |
| |
| |
| Question |
How can we handle runtime exceptions? write one sample
program? Can we write runtime exceptions beside the throws
key word? if yes write sample program? |
Rank |
Answer Posted By |
|
Question Submitted By :: Seenu |
| This Interview Question Asked @ Huawei |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | there is no need to handle a runtime exception,
but acording to question
there are two types through which we could handle the
exception one is decalre the exception and other try catch
block.
so for this i'll show you the customize way for this.
class AmitSingh extends RuntimeException
{
}
class Amit
{
int number;
Amit(int number)
{
this.number = number;
}
public void method1()
{
try
{
if(number>5)
throw new AmitSingh();
}
catch(AmitSingh e)
{
System.out.println(e.printStackTrace());
}
public static void main(String []args)
{
Amit a = new Amit(20);
a.method1();
}
}
but i don't think there is need to this because jvm is
responsible for all that thanks AMIT SINGH09  |
| Amit2009mca |
| |
| |
| Question |
I have an HashMap object, which has with key and value pair.
It has 10 keys and values in that object. Now the question
is I want insert new key and value in middle or any where in
that list but not at the end or at the top. Is it possible
or not. If yes how can we achieve this one? |
Rank |
Answer Posted By |
|
Question Submitted By :: Seenu |
| This Interview Question Asked @ Huawei |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | It is not in the programmers hand to decide location of any
key/value pair in hashmap.These pairs are randomly placed in
Hashmap based on some calculated hash codes.  |
| Alka |
| |
| |
| Question |
What is the difference between throw and throws? |
Rank |
Answer Posted By |
|
Question Submitted By :: Ridhima |
| This Interview Question Asked @ Cap-Gemini |
|
I also faced this Question!! |
© ALL Interview .com |
| Answer | throw:it is used to raise exception explicitly
that means it is use when a user defined exception is raised
throws:if a method is capable of throwing an exception but
it does not handle the exception that must be specified by
using "throws" class  |
| Balaji |
| |
| |
| Answer | Throw clause is used in any part of the code where you want
to throw any specific exception to the calling method while
throws can be termed as a substitute for try-catch block.
When an exception occurs in a program, the compiler needs to
know what should be done with that exception. For that
either we should have a try-catch block or the method should
have the throws clause attached, as follows.
<access-modifier> <return-type> myMethod(<parameter-list>)
throws <exception-list>
for example, public void meth1() throws MyException
throw can handle only ONE throwable object while throws can
handle multiple throwable objects seperated by commas.  |
| Rakesh Nath |
| |
| |
|
| |
|
Back to Questions Page |