• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

4 questions from valiveru mock test

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Question 11.
Select the range of 'char'
A.-(2 power 7) TO (2 power 7)-1
B.-(2 power 15) TO (2 power 15)-1
C.0 TO (2 power 16)-1
D.0 TO 255
They gave C and D as correct. Isnt it 0 TO (2 power 15)-1 ?
Question 18.
Given the following class definition in a file named MyClass.java,

import java.io.IOException;

public class MyClass{
int i;
public void amethod(){

try{
i = 10;
i++;
throw new IOException();
i--;
}
catch( IOException e){
i++;
}
finally{
i--;
System.out.println("i is : "+i);
}
} //end of amethod()

public static void main(String[] args){
MyClass mc = new MyClass();
mc.amethod();
} //end of main
} //end of class MyClass
What happens when we try to compile and run MyClass.java.
A.Code does't compile because of error Statement not
reached.
B.Code doesn't compiles because IOException cann't be
thrown explicitly
C.Code compiles & runs with output
i is : 11
D.Code compiles & runs with no output.
The correct answer is A. The error is at the line following the Throw statement.
I was totally unaware this would cause an error.
Question 29.
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();
}
}
They said C and D but I dont see anything wrong with B.
Question 34.
Which of the following are true about inner classes

A.Inner classes can only be instantiated in its outer class
B.Inner classes can be protected or private
C.Anonymous inner class can be both an explicit subclass
and implements an interface.
D.Anonymous inner classes declare and instantiated at the
same place.
E.An inner class can be static only when it's outer class
is static.
F.An instance of an Anonymous inner class can only be
created in it's outer class
They said B and D but I chose F as well.
[\quote]

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For qustion 11
----------
the range of a char is 0 to 2^16 - 1. (look at your books)
Question 18
----------
I saw no problem with this code, can you tell me what exactly is wrong?
Question 29
-----------
I thought only D was correct, how can you C and B be correct if they dont declare all the checked exceptions that can be thrown ??
question 34
_____________
F is wrong, there is no such rule.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled 18 and it indeed gave me a compile time error. Statement not reached i--
question 29 only BaseException and SubException are checked exceptions and since a SubException IS a BaseException answer C is ok. I still dont see what is wrong with B.
As for question 34 answer F. consider the following:
Anonymous inner classes can only be instantiated at the same point they are declared, like this:
return new ActionListener()
{ public void actionPerformed(
ActionEvent e); { }
};

[This message has been edited by Randall Twede (edited December 16, 2000).]
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
#11 - you absolutely right.
JLS
4.2.1 Integral Types and Values
The values of the integral types are integers in the following ranges:
For byte, from -128 to 127, inclusive
For short, from -32768 to 32767, inclusive
For int, from -2147483648 to 2147483647, inclusive
For long, from -9223372036854775808 to 9223372036854775807, inclusive
For char, from '\u0000' to '\uffff' inclusive, that is, from 0 to 65535

 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am taking another test now and it says the range of short type -(2^15) to 2^15-1 and the range of char is O to 2^16-1
I understand that one now. since they are both 16 bit and one has a sign bit, it makes sense now.
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"An instance of an Anonymous inner class can only be created in it's outer class." is not correct, may be this example will explain that to you
class sup{
void smethod(){}
}
class cage{
static sup method(){
return new sup(){
void smethod()
{ System.out.println( "from anonymous class");}
};
} }

class calling{
public static void main(String st[]){
cage.method().smethod();

}
}

[This message has been edited by Nasir Khan (edited December 16, 2000).]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is a good example it is clear that an instance of the anonymouse class is created in class calling. I just wont worry about this statement:
"Anonymous inner classes can only be instantiated at the same point they are declared"
since it confused me.
so that leaves only question 29 in doubt. I think B is a correct answer also. there is nothing wrong with including a RuntimeException in the throws clause.
to make sure i wrote this program:

[code]
import java.io.*;
class Throws {
public static void main(String[] args) {
try {
Throws t = new Throws();
t.aMethod();
}
catch(IOException e) {
}
}
void aMethod() throws IOException,RuntimeException {
System.out.println("HI");
}
}
[\code][\quote]
it compiles and runs fine.
[This message has been edited by Randall Twede (edited December 16, 2000).]
[This message has been edited by Randall Twede (edited December 16, 2000).]
[This message has been edited by Randall Twede (edited December 16, 2000).]

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. "An instance of an anonymous or local inner class can only be created within the scope of its declaration "
This statement is true.
2. "An instance of an anonymous or local inner class is accessible only within the scope of its declaration "
This statement is false.
Creating an object is not the same as passing a reference to an object around.
Cheers
Sahir
[This message has been edited by Sahir Shah (edited December 16, 2000).]
 
Vladimir Kositsky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#29
JLS

11.2.2 Why Runtime Exceptions are Not Checked
The runtime exception classes (RuntimeException and its subclasses) are exempted from
compile-time checking because, in the judgment of the designers of Java, having to
declare such exceptions would not aid significantly in establishing the correctness of Java
programs. Many of the operations and constructs of the Java language can result in
runtime exceptions. The information available to a Java compiler, and the level of
analysis the compiler performs, are usually not sufficient to establish that such runtime
exceptions cannot occur, even though this may be obvious to the Java programmer.
Requiring such exception classes to be declared would simply be an irritation to Java
programmers.
For example, certain code might implement a circular data structure that, by
construction, can never involve null references; the programmer can then be certain that
a NullPointerException cannot occur, but it would be difficult for a compiler to prove
it. The theorem-proving technology that is needed to establish such global properties of
data structures is beyond the scope of this Java Language Specification.


[This message has been edited by Vladimir Kositsky (edited December 17, 2000).]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ,

Valiveru Mock Exam Question :
Question 34.
Which of the following are true about inner classes

A. Inner classes can only be instantiated in its outer class
B. Inner classes can be protected or private
C. Anonymous inner class can be both an explicit subclass
and implements an interface.
D. Anonymous inner classes declare and instantiated at the
same place.
E. An inner class can be static only when it's outer class
is static.
F. An instance of an Anonymous inner class can only be
created in it's outer class
They said B and D but I chose F as well.

The following post makes sense.

Originally posted by Sahir Shah:
1. "An instance of an anonymous or local inner class can only be created within the scope of its declaration "
This statement is true.
2. "An instance of an anonymous or local inner class is accessible only within the scope of its declaration "
This statement is false.
Creating an object is not the same as passing a reference to an object around.



So I choose B,D and F.
Can any moderators confirm this?
Thanking you Rosemol.

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nasir Khan:
"An instance of an Anonymous inner class can only be created in it's outer class." is not correct, may be this example will explain that to you
class sup{
void smethod(){}
}
class cage{
static sup method(){
return new sup(){
void smethod()
{ System.out.println( "from anonymous class");}
};
} }

class calling{
public static void main(String st[]){
cage.method().smethod();

}
}

[This message has been edited by Nasir Khan (edited December 16, 2000).]


The program was a good explanation for the question and it is good example of where we can use an inner class
 
Rosemol Thanjappan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am little bit confused.
As far as I know

The code
[B]

means you are extends the class sup and overriding the method void smethod() and creating the instance of that extended class at the same place.
That is same as
[B]

From the class calling your calling the method by
[B]
When smethod is called
In the method smethod() inside the class cagecreate an Instance of extended sup class and returns it to class calling.
This is my understanding .
Correct me if I am wrong.
and could you Please explain at what line the anonymouse inner class subclass of sup is created outside its enclosing class calling ?

Thanking you
Rosemol
 
Get out of my mind! Look! A tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic