Questions tagged [scjp]

SCJP is a certification for programmers experienced using the Java programming language. SCJP affirms that the programmer really knows how to code in Java. However the ability to design and implement a complete application is not affirmed by this test (it is affirmed by the second step certification, SCJD).

SCJP (Sun Certified Java Programmer) is a certification for programmers experienced using the Java programming language. Achieving this certification provides clear evidence that a programmer understands the syntax and structure of the Java programming language and knows enough about J2SE system library.

During the test, the programmer needs to solve multiple short programming tasks in a limited time. Unlike the second level (SCJD) test, the SCJP test does not require to implement and demonstrate a complex working application.

285 questions
211
votes
7 answers

What are "connecting characters" in Java identifiers?

I am reading for SCJP and I have a question regarding this line: Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! It states that a…
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
162
votes
10 answers

Why does Double.NaN==Double.NaN return false?

I was just studying OCPJP questions and I found this strange code: public static void main(String a[]) { System.out.println(Double.NaN==Double.NaN); System.out.println(Double.NaN!=Double.NaN); } When I ran the code, I got: false true How…
Maverick
  • 3,053
  • 6
  • 24
  • 30
73
votes
2 answers

Is id = 1 - id atomic?

From page 291 of OCP Java SE 6 Programmer Practice Exams, question 25: public class Stone implements Runnable { static int id = 1; public void run() { id = 1 - id; if (id == 0) pick(); else …
Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
34
votes
3 answers

Formatting using printf and format

In the following program class ZiggyTest2 { public static void main(String[] args){ double x = 123.456; char c = 65; int i = 65; System.out.printf("%s",x); System.out.printf("%b",x); …
ziggy
  • 15,677
  • 67
  • 194
  • 287
34
votes
3 answers

Why can not I add two bytes and get an int and I can add two final bytes get a byte?

public class Java{ public static void main(String[] args){ final byte x = 1; final byte y = 2; byte z = x + y;//ok System.out.println(z); byte a = 1; byte b = 2; byte c = a + b; //Compiler…
Joe
  • 7,749
  • 19
  • 60
  • 110
28
votes
3 answers

How to call a specific parent constructor from anonymous inner class?

Ok, So I know that an anonymous inner class is either implicitly extending a parent class or implementing an interface, and therefore a constructor of the superclass will need to be called. However, I'm not sure how to create a constructor for the…
paniclater
  • 903
  • 1
  • 12
  • 18
28
votes
3 answers

How can't we compare two enum values with '<'?

If enum implements Comparable so why can't compare with < or >? public class Dream { public static void main(String... args) { System.out.println(PinSize.BIG == PinSize.BIGGER); //false System.out.println(PinSize.BIG ==…
Joe
  • 7,749
  • 19
  • 60
  • 110
24
votes
4 answers

Java arrays - Why is the output '1' ?

Why is the output in this example 1? public static void main(String[] args){ int[] a = { 1, 2, 3, 4 }; int[] b = { 2, 3, 1, 0 }; System.out.println( a [ (a = b)[3] ] ); } I thought it would be 2. i.e., the expression is evaluated…
ziggy
  • 15,677
  • 67
  • 194
  • 287
24
votes
2 answers

Why do we use final keyword with anonymous inner classes?

I'm currently preparing the S(O)CJP, with the Sierra & Bates book. About inner classes (method local or anonymous), they say that we can't access the local variables because they live on the stack while the class lives on the heap and could get…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
24
votes
7 answers

Use of Serializable other than Writing& Reading object to/from File

In Which Cases it is a good coding practice to use implements serializable other than Writing & Reading object to/from file.In a project i went through code. A class using implements serializable even if in that class/project no any Writing/Reading…
Anurag_BEHS
  • 1,390
  • 2
  • 22
  • 36
24
votes
4 answers

Java SneakyThrow of exceptions, type erasure

Can someone explain this code? public class SneakyThrow { public static void sneakyThrow(Throwable ex) { SneakyThrow.sneakyThrowInner(ex); } private static T sneakyThrowInner(Throwable ex) throws…
Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419
24
votes
8 answers

How to programmatically test if assertions are enabled?

One of the correct answers from OCP Java SE 6 Programmer Practice Exams is: You can programmatically test wheather assertions have been enabled without throwing an AssertionError. How can I do that?
Joe
  • 7,749
  • 19
  • 60
  • 110
23
votes
3 answers

Java char to byte casting

I have been testing the char casting and I went through this: public class Test { public static void main(String a[]) { final byte b1 = 1; byte b2 = 1; char c = 2; c = b1; // 1- Working fine c = b2; // 2…
oueslatibilel
  • 567
  • 1
  • 8
  • 24
23
votes
2 answers

Bitwise shift operators. Signed and unsigned

I'm practising for the SCJP exam using cram notes from the Internet. According to my notes the >> operator is supposed to be signed right shift, with the sign bit being brought in from the left. While the left shift operator << is supposed to…
user271052
19
votes
3 answers

Enum as instance variables

If you have an enum such as enum Coffee { BIG, SMALL } and a class that has an instance variable like this of the enum: public class MyClass { private Coffee coffee; // Constructor etc. } Why is it possible in the constructor to…
LuckyLuke
  • 47,771
  • 85
  • 270
  • 434
1
2 3
18 19