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

Questions from Dan and Green's Exams..

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK This q was from Dan's exam..
I'm wondering as in a lot of questions.. we may need to use a char as int like in this q.. are we supposed to know what is the representation of 'a' as an int?? Am I missing something..
class Sienna {
static double a;
static float b;
static int c;
static char d;
public static void main(String[] args) {
a = b = c = d = 'a';
System.out.println(a+b+c+d == 4 * 'a');
}
}

What is the result of attempting to compile and run the above program?
a. Prints: true
b. Prints: false
c. Compiler error.
d. Run time error.
e. None of the above.
THIS question is from Green's mock exam.. The correct answer includes the choice 1, i chose only 2 and 4. the first one wont that give a compile error coz the assignment operator has least precedence this means that 1/3 evaluated first. the value will be a decimal which by default will be considered double and assigning double to float is a compile error!
Which of the following are legal statements?
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;

Comments appreciated!! Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Marcus Green's question option 1): 1/3 gives an int result of 0. That gets converted to 0.0f when assigned to a float.
In Dan's question: char 'a' is in a numeric range which can be converted to int, float, double without losing or gaining any precision, the addition a+b+c+d will give an exact result too. 4 * 'a' will be performed resulting in an int that can be converted to an exact double. So I would say that a+b+c+d == 4 * 'a' results in true. I just had to know that a is in the range 0 to 2^16-1.
-Barry
[ October 20, 2002: Message edited by: Barry Gaunt ]
[ October 20, 2002: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Saniya,
As usual, Barry is correct. The question was designed such that you don't need to know the numeric value of 'a'. You only need to know that the implicit type conversion is performed as the primitive char value is converted to an int, float and double.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only the 'easy' ones Dan.
I'm getting busted on the "Cattle Drive". I can only hope it gets better after Halloween...
-Barry
 
If you have a bad day in October, have a slice of banana cream pie. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic