jQuery in Action, 2nd edition*
The moose likes Mock Exam Errata and the fly likes 5 Conceptual Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "5 Conceptual Questions" Watch "5 Conceptual Questions" New topic
Author

5 Conceptual Questions

Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Hello and Greetings,
Please explain the following:

1) public class Emp{
int i[] = {0};
public static void main(String args[]){
int i[] = {1};
change_i(i);
System.out.println(i[0]);
}
public static void change_i(int i[]){
int j[] = {2};
i = j;
}
}
If arrays behave like objects the answer should be 2. But the correct answer is 1. Why?

2. Is the following legal ( ie,Are two package statements legal?).
package pkg;
package pkg2;
import java.awt.*;
class c{};

3. I think following should give error because we are trying to put int in to char with out cast. But it compiles fine. Why?
char c = 32; //is Legal.

4. byte x = 3;
x = ( byte ) ~ x ;
System.out.println(x);

I think x can be -4 as well as 252, but the correct answer is -4. Please explain.

Reason for 252:
Binary of byte x = 3: 00000011
~3 = 11111100 = 252

5. Does delete() in the File class actually delete a file or just deletes an instance of the File Class?
Regards
Vineet
abhijit r
Greenhorn

Joined: Mar 07, 2001
Posts: 4
Vineet,
1) The System.out.println inside main() prints out the value of the LOCAL variable i[0], whereas the one modified in change_i is the CLASS variable i[0], which does in fact get set to 2. To check this, define another class variable int x[1], and in change_i, set x = j. In main(), print x[0], which will be set to the value 2.
2) There can be only one package statement per file.
3) I am guessing that the right side is not promoted until the entire expression is evaluated - so 32 is still a byte? Anyone?
4) The range of values for a byte (8 bits) is -128 to +127. Any value with the highest bit set to 1 is considered as a negative value - the highest bit ALWAYS indicates sign in java (in the case of signed types, that is).
5) delete() deletes the actual file referred to by the File object. Check Sun's documentation at http://java.sun.com/products/jdk/1.0.2/api/java.io.File.html#3182
-Abhijit
Vineet Sharma
Ranch Hand

Joined: Dec 30, 2000
Posts: 51
Thank You Abhijeet
Veer Batra
Ranch Hand

Joined: Mar 12, 2001
Posts: 35
Regarding 3 above :
Here is the rule for literal values assigned to byte, char, short which says that literal int value is cast implicitly for these types as well as in combined declaration and initialization. Plesae take care if we assign char i= j ( where char j= 32), this expression will give error because j is not literal so implicit cast rule is not valid.
flle free if I am not correct.
Regards,
Veer
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Veneet,
I think Abjihit's answer to number 1 is correct, however his reasoning is way off. The class variable i never gets changed in your example. In both methods it is hidden by a local or passed variable of the same name. In java, you can't change the reference of any passed variable. That is the REASON that 'i' remains the same as it was before the change_i method was called.
Arrays differ from other objects in that you can swap or change its contents. If your change_i would look like the following then the answer would be 2:

If the class variable 'i' was not hidden by local variables then the compiler would complain because 'i' is an instance variable and therefore not accessible from within static methods.
Regards,
Manfred.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: 5 Conceptual Questions