• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Java Interview Questions

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1)char c = 127; c = c + 10;
Which of the following is true
a) c > 0b) c = 0c) c < 0d) c >= 0
-----------------------------------------------------
2)int a = 3^1; //Bitwise XOR operation
a = ?
a) 0b) 1c) 2d) 3
-----------------------------------------------------
3)int foo(int a){ return a++ * ++a; } What�s the result of foo(5) ?
a) 35b) 42c) Compilation Errord) 30
-----------------------------------------------------
4)class A{public static void print(){System.out.print(�A�);}},
class B extends A{public static void print(){System.out.print(�B�);}};
A a = new B();
What will a.print() print ?
a) Ab) Bc) Compilation Errord) Runtime Error
-----------------------------------------------------
5)int i = 1;
try{ i++; throw new Exception();}
catch (Exception ex ) { i++; throw ex;}
finally{ i++; System.out.print(i)}; What will this print, if anything ?
a) Nothing.b) 2c) 3d) 4

-----------------------------------------------------
My answer:
1) A 2)C 3)A 4)B 5)D

Anyone can check these for me? Thanks!
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answers were: 1)A 2)C 3)A 4)C 5)D
What kind of job was it , did they tell you the answers?
These questions seem kind of easy for interview questions. Hope you got the job.
 
Kevin Black
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply.

This is a screen test from some recruiter, and I couldn't believe the answer she showed me is:
C C D A C

Just couldn't believe I missed that much...
[ September 17, 2007: Message edited by: Kevin Black ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have a computer with a Java SDK installed, you can very quickly find the correct answers to these questions by writing a few small test programs.

1) This code doesn't even compile! You're adding an int (10) to a char. The result of that computation is an int, which doesn't fit in a char. Even if you cast the result to a char, it is never going to be < 0. The type char is unsigned in Java, so it can never be < 0. So answer C is wrong!

2) 3 XOR 1 = 2, so answer C.

3) Just try it out: 35, answer A.

You begin with: a = 5
Then a++ is evaluated; the result is 5 (the value of a before the increment), and a is incremented to 6
Then ++a is evaluated; the result is 7 (the value of a after the increment), and a is incremented to 7
The result is 5 * 7 = 35

4) It will print "A" (answer A), because the print() method is static and static methods are not polymorphic.

5) You misplaced a ; so this will not compile. However, if that's fixed it will print 4 (answer D). So answer C is wrong! First, the i++ in the try block is executed, then the i++ in the catch block, then the i++ in the finally and then the value is printed. First i was 1, then it has been incremented three times so the value is 4 when it is printed.

So the answers C C D A C are not entirely correct!

Did you copy the questions correctly? Probably you meant 'byte' instead of 'char' at question 1, and maybe it should have been 'int i = 0;' instead of 'int i = 1;' in question 5.
[ September 17, 2007: Message edited by: Jesper Young ]
 
Jay Dilla
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kevin Black:
Thanks for your reply.

This is a screen test from some recruiter, and I couldn't believe the answer she showed me is:
C C D A C

Just couldn't believe I missed that much...

[ September 17, 2007: Message edited by: Kevin Black ]




Wow fooled me too. What did she say about you getting them wrong?
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answers were:
1 c(*)
2 c
3 a
4 a - maybe c (compilation error) because you have typographic “” instead of regular "".
5 n - rethrow is neither declared nor caught, so it does not compile. From that, can we conclude that it doesn't print anything?
And in 5 also a } and a ; are swapped.


(*) c is wrong. They fooled me with this, it does not compile...



Yours,
Bu.
 
Kevin Black
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your detailed comments, Jesper!

1) I think this questions sucks, too! It should throwing an type conversion error but there is no such answer in the choices. I think it's trying to test the char data type range and type conversion, maybe?!

5) Believe me, the ";" is in the wrong place in the original question, I fixed that as well.

I did copy/paste the question right after double check them. And I did wrote a quick main() to test these questions for all these.

Thanks for the help! I will call the recruiter and ask them to correct the answer and stop confusing people.

Also, I will post more interview questions from my upcoming interviews.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic