Shyamala S

Greenhorn
+ Follow
since Sep 12, 2000
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Shyamala S

Hi
These are questions found in http://valiveru.tripod.com/java/jvaltest.html, at the end of each question pls loook for my questions and doubts and kindly clarify them.
Question 7.

Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy

java.lang.Exception
|
+ - - BaseException
|
+ - - SubException
|
+ - - java.lang.RuntimeException

Which of the following are legal

A.public class MyClass {
public void myMethod(){
aMethod();
}
}

B.public class MyClass{
public void myMethod() throws BaseException,RuntimeException{
aMethod();
}
}

C.public class MyClass{
public void myMethod() throws BaseException{
aMethod();
}
}

D.public class MyClass{
public void myMethod() throws Exception{
aMethod();
}
}

E.public class MyClass{
public void myMethod() throws RuntimeException {
aMethod();
}
}
c,d
//////////////////////////
is b not legal ?? why ???
///////////////////////////
==================================================

Question 13.

Select the valid primitive assignments of the following.
A.int i = 10;
char c = i;

B.float f;
long l = 100L;
f = l;

C.short s = 20;
char c = s;

D.byte b = 20;
char c = b;

E.short s1 = 10;
short s2 = 20;
short result = s1*s2;

a,b
/////////////////////////////////
why is d not valid ???
////////////////////////////////
================================================
Question 28.

How many String objects are created when we run the following code.

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;
A.1

B.2

C.3

D.4

E.We can't say.

c
////////////////////////////////
i think b is right bcoz two strings are created
one at s1="Hello" and one at s3=s2+"Pal"
//////////////////////////////
================================================
Question 43.

Which of the follwing is true about static modifier.

A.static can be applied to : instance variables, methods,
code Segments and classes.

B.a static method cannot be overridden.

C.inner classes can be both static & private.

D.a static reference cannot be made through non static
method or code block.

E.abstract & static both can be applied together to a
method.
b,c,d
/////////////////////////////
how is d right ?? actually the viceversa is right ie. non static reference
cannot be made thro static method or code block
//////////////////////////////
===========================================================
Question 48.

Which of the following are true about Layout Managers

A.The component gets its preffered size when we use
FlowLayout to layout the components of container.

B.In BorderLayout, the component at center gets all the
space that is left over, after the components at North &
South have been considered.

C.The method setRowmajor(boolean) is used to set the way
components are layed out(Row/Column after Row/Column)
while using GridLayout.

D.Programmers can use a layout called null layout to place
the components at the coordinate positions he wants.

E.With a GridLayout Manager if they are two many components
to fit in a single row, additional rows are created.

b,d
//////////////////////////////////////////////////
i think there is an error with the answer markings
bcoz d is wrong and e is right.
////////////////////////////////////////////
=====================================
hi marcus,
i too feel the same as Ramesh. i couldnt at the first glance of the explanation note that it is becoz of the method not available in base class... probably you could modify the explanation a bit.
thanks both of you. i now understood this clearly.
Question 25)
Which of the following statements are true?
1) The following statement will produce a result of 1. System.out.println( -1 >>>2);
2) Performing an unsigned left shift (<<<) on a negative number will always produce a negative number result <br /> 3) The following statement will produce a result of zero, System.out.println(1 >>1); <br /> 4) All the integer primitives in java are signed numbers <br /> <br /> Answer to Question 25) <br /> Objective 5.1)<br /> 3) The following statement will produce a result of zero, System.out.println(1 >>1);
Although you might not know the exact result of the operation -1 >>> 2 a knowledge of the way the bits will be shifted will tell
you that the result is not plus 1. (The result is more like 1073741823 ) There is no such Java operator as the unsigned left shift. Although it is normally used for storing characters rather than numbers the char Java primitive is actually an unsigned integer
type.
============
my doubt is the option 4 specifically asks for integer primitives and the answer explains about char Java primitive ....is char an integer primitive ??
-------------------------------------------
Question 57)
Given the following code
class Base {}
class Agg extends Base{
public String getFields(){
String name = "Agg";
return name;
}
}
public class Avf{
public static void main(String argv[]){
Base a = new Agg();
//Here
}
}
What code placed after the comment //Here will result in calling the getFields method resulting in the output of the string "Agg"?
1) System.out.println(a.getFields());
2) System.out.println(a.name);
3) System.out.println((Base) a.getFields());
4) System.out.println( ((Agg) a).getFields());
Answer
4) System.out.println( ((Agg) a).getFields());
The Base type reference to the instance of the class Agg needs to be cast from Base to Agg to get access to its methods.The
method invoked depends on the object itself, not on the declared type. So, a.getField() invokes getField() in the Base class,
which displays Base.
------
Here Marcus says that method invoked depends on the object itself, not on the declared type, which means the object which is an Agg will invoke its method not the method of the declared type Base....am i right in this understanding, pls explain.
yes both the files are in the same directory.

this is actually a snippet from the website http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/
in the link first servlets... section 4 simple html building utilities.
kindly look into it and clarify my doubt.
thank you
23 years ago
Hi
It has been included already. Actually i think this is not a problem related to servlets but to general static methods in java classes.
23 years ago
given two classes in seperate source files, the first one compiles without error, but the second file on compilation gives the following error
<pre>
Undefined variable or class name: ServletUtilities
out.println(ServletUtilities.headWithTitle("Hello WWW")
+ "< BODY>\n" + "< H1>Hello WWW< /H1>\n" + "< /BODY>< /HTML>");</pre>
what is the error ??? kindly help.
thankyou
shyamala.
<code><pre>public class ServletUtilities{
public static final String DOCTYPE =
"< !DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">";
public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"< HTML>\n" +
"< HEAD>< TITLE>" + title + "< /TITLE>< /HEAD>\n");
}

// Other utilities will be shown later...
}
=======================

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWWW2 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(ServletUtilities.headWithTitle("Hello WWW")
+ "< BODY>\n" + "< H1>Hello WWW< /H1>\n" + "< /BODY>< /HTML>");
}
}</pre></code>
[spaces added after every '<' to force parsers to display tags rather than interpret them - Jim]
[This message has been edited by Jim Yingst (edited September 14, 2000).]
23 years ago
28. Given the following code listing:
public class myClass{

//Your Declaration HERE!{
try {
System.in.read();
} catch (IOException e) {
return;
}
}
public static void main(String args[]) {
System.out.println("My console application");
System.out.println("");
System.out.println("(press Enter to exit)");
waitForKey();
}
}

At the specified location, provide a method declaration with the following properties:
Name: waitForKey
Input Parameters: None
Return Value: None
Visibility: All classes, including subclasses, in the current package.

i thought the answer was protected void waitForKey() but the answer is given as public void waitForKey().... how ??
===============================================
78. What is the output of the following try�catch block?
public class tryme {
public static void main(String args[]) {

try{
int I;
throw(new Exception("Hello Nurse");
return;
}catch(Exception e){
System.out.println("I am Here");
return;
}finally{
System.out.println("Hello Nurse!");
}

}
}
A. Hello Nurse!
B. No output
C. I am Here
D. A and C
I think the answer shud be D whereas it is given as C, pls explain
http://www.javaprepare.com/quests/operat_q.html
24.Which all lines are part of the output when the following code is
compiled and run. Select all correct answers.

public class test {
public static void main(String args[]) {
for(int i = 0; i < 3; i++) {
for(int j = 3; j <= 0; j--) {
if(i == j) continue;
System.out.println(i + " " + j);
}
}
}
}

A.0 0
B.0 1
C.0 2
D.0 3
E.1 0
F.1 1
G.1 2
H.1 3
I.2 0
J.2 1
K.2 2
L.2 3
M.3 0
N.3 1
O.3 2
P.3 3
Q.The program does not print anything.
correct answer is q, how ??? is it bcoz the second loop checks for j<=0 ???
thanks guys, this is really news to me