• 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:

5 Conceptual Questions

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Abhijeet
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
There's a city wid manhunt for this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic