• Post Reply Bookmark Topic Watch Topic
  • New Topic

Re: Few Questions from Abilash's mock exam  RSS feed

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.public class AQuestion
{
private int i = j;
private int j = 10;
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers
1.Compiler error complaining about access restriction of private variables of AQuestion.
2.Compiler error complaining about forward referencing.
3.No error - The output is 0;
4.No error - The output is 10;
The correct answer is 2
2.public class AQuestion
{
private int i = giveMeJ();
private int j = 10;
private int giveMeJ()
{
return j;
}
public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}
Answers
1.Compiler error complaining about access restriction of private variables of AQuestion.
2.Compiler error complaining about forward referencing.
3.No Compilation error - The output is 0;
4.No Compilation error - The output is 10;
The correct answer is C
I don't understand why in Q#2 the answer is 3 instead of 2.
3. public class ADirtyOne
{
//char a = '\u000A';
}
An attempt to compile the above class
1. will complete successfully.
2. will compile sucessfully but with a warning message.
3. will not compile - complains on an invalid expression.
The correct answer is 3. Since we have comment out the line
//char a = '\u000A';
I thought the answer is 1.
4.Read this piece of code carefully
if("String".replace('g','G') == "String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");

Answers
1.the code will compile an print "Equal".
2.the code will compile an print "Not Equal".
3.the code will cause a compiler error
The correct answer is 2 but I thought it is 1.
5.import java.io.*;
public class TransientWriter implements Externalizable
{

private transient String s = "Hope I can ever be persistant!";
public void writeExternal(ObjectOutput oOut) throws IOException
{
oOut.writeObject(s);
}

public void readExternal(ObjectInput oIn) throws IOException,
ClassNotFoundException
{
s=(String)oIn.readObject();
}
public String toString()
{
return s;
}
}
class K
{
public static void main(String args[]) throws IOException,
ClassNotFoundException
{
TransientWriter tw = new TransientWriter();
ObjectOutputStream out = new ObjectOutputStream(new
FileOutputStream("tw.out"));
out.writeObject(tw);
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("tw.out"));
TransientWriter tw2 = (TransientWriter) in.readObject();
System.out.println(tw2);
}
}
Attempting to compile and run the above code

1.will cause a compiler error due to the attempt to write a transient object.
2.will cause a runtime exception when an attempt is made to write a transient object.
3.will not cause any runtime error and the transient object is writen to the file named "tw.out".
4.will not cause any runtime error and the transient object is not written to the file named "tw.out". The
program prints a blank line on the screen.
The correct answer is 3 but I thought it was 4.
Can anyone help me with this.
Thanx


 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer to your first question:
http://www.javaranch.com/ubb/Forum24/HTML/001464.html
[This message has been edited by Vidya Selvaraj (edited December 03, 2000).]
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Punitha,
Q2. The answer is #3 vs #2 because instance variables are initialized when the class is loaded; before any methods are invoked. As 'i' is calling a method, the assignment doesn't occur until the class is fully loaded. Then the method is executed a '0' is returned as default value of 'j'.
Variables are initialized in the order they appear in the code. In Q1, you're trying to initialize 'i' with a value that doesn't yet exist so you get the error.
Q3. Unicode characters are translated literally, before any strings or other items are evaluated; so commenting out the line doesn't work.
Q4. The string method replace does not actually modify the original string; instead it creates a new string object, returning a reference to the new object. It does not check the string pool to see if a matching object already exists. The code is actually trying to compare two newly created, separate objects.
Q5. Not 100% sure on this one but, while the default implementation of Serializable will not write out transient objects, Externalizable allows you to provide your own implementation, which, in this example, writes out the transient object.
Hope that helps.
Just a note, in future would mind posting one question per thread. It makes the questions easier to track.
Thanks.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
Punitha krishna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane and Vidya
Thanks for replying. Still I don't understand question 3 about unicode and also question 5 regarding IO.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Punitha,
Q3 relies on a rule as given in the JLS


Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into
an actual linefeed in translation step 1 (�3.3) and the linefeed becomes a LineTerminator in step 2 (�3.4), and so the character literal is not valid in step 3. Instead, one should use
the escape sequence '\n' (�3.10.6). Similarly, it is not correct to write '\u000d' for a character literal whose value is carriage return (CR). Instead, use '\r'.


You just have to remember not to use them
Q5. The code overides writeExternal(), causing the transient variable to be written to output.
Hope that helps.
------------------
Jane
The cure for boredom is curiosity.
There is no cure for curiosity.
-- Dorothy Parker
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!