Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2 questions from Mock Exams.

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1) What will happen when you compile this code?
public class Ab3Q29
{
private final int i =10;
private byte k = i;
}
Answer 1:
compiles cleanly.
Why? According to Robert Heller's book, when a literal int value is assigned to a narrower primitive type (byte,short,char) , it's ok as long as the literal value falls within the range of the primitive type. However, this is not true, if you assign a variable as in the code above. He even gives an ex:
int i = 12;
byte b = i; // will not compile
Q2) What happens when you compile this?
public class A
{
// char a = '\u000A';
}
I tried it and it actually gives 2 compiler errors as follows :
"Invalid character constant" and
"Type expected"
Why is it looking inside a comment? Also when declaring a char literal, does it have to be hex digits only and not the hex alpha A,B,C,D,E,F, because, when I changed the above value to '\u0009', it compiled.
I am getting really worried about the exam now. There are so many little things that I don't know and I am now wondering if I did not make the right choice of books.
For exam purposes, am I supposed to know, every method, constructor, hierarchy etc?
Thanks, in advance, for your help
Sharda
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens when you compile this?
public class A
{
// char a = '\u000A';
}

char a = '\u000A'. Why is this invalid?
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but why would the compiler complain when it's in a comment?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As for question 1, the difference between the two examples is the word "final". The compiler doesn't remember what value has been stored in an ordinary int variable, but it does know at all times what's in a compile-time constant (final) variable.
 
Sharda Vajjhala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ron, that makes sense. When I removed "final" it gave me a "explicit cast required" error.
Regarding the other question, consider this:
public class A
{
/* char a = '\u000A'; */
char c = '\u000C'; // np char - what is this ??
char d = '\u000D'; // carriage return
char e = '\u001B'; // esc
char f = '\u007F'; // del char

}
This DOES NOT give an error for the comment line, when I changed it to /* .... */ comment!!! I'm going nuts!
Also, why does it complain only about '\u000A' and '\u000D'? It does not complain about the esc and del chars in the above program.
Any help is greatly appreciated. Just when I think I have understood something, I am proven wrong.
Thanks
Sharda
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In case of a single line comment,
// char c = '\u000A';
it would like this...
// char c = '
';
Thats why the compilation errors.
In case of a block comment
/* char c = '\u000A' */
it would like this ...
/* char c = '
' */
A valid block comment.
Can somebody confirm this please?
I don't know which version you are using, but i didn't get any errors for '\u000D' in JDK 1.2.2
but i got an error in JDK 1.4.1. This is interesting.
[ September 23, 2002: Message edited by: Vin Kris ]
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vin, you are correct.
As to the questions... question 1 is something you should definitely know. I don't think anyone has ever seen question 2 or anything like it on the certification exam.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic