Questions tagged [ocpjp]

OCPJP (Oracle Certified Professional, Java Programmer), formerly Sun Certified Java Programmer (SCJP), is a certification for programmers experienced using the Java programming language. Achieving this certification provides clear evidence that a programmer understands the basic syntax and structure of the Java programming language.

http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=320&p_org_id=28&lang=US

80 questions
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
45
votes
15 answers

a = (a++) * (a++) gives strange results in Java

I'm studying for the OCPJP exam, and so I have to understand every little strange detail of Java. This includes the order in which the pre- and post-increment operators apply to variables. The following code is giving me strange results: int a =…
Marius
  • 57,995
  • 32
  • 132
  • 151
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
1 answer

java.util.Arrays.asList when used with removeIf throws UnsupportedOperationException

I am preparing for an OCPJP 8 exam for the next 2 months and currently I this one got my attention as I dont understand why public class BiPredicateTest { public static void main(String[] args) { BiPredicate, Integer>…
mark ortiz
  • 659
  • 1
  • 6
  • 13
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
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
15
votes
3 answers

How does string.split("\\S") work

I was doing a question out of the book oracle_certified_professional_java_se_7_programmer_exams_1z0-804_and_1z0-805 by Ganesh and Sharma. One question is: Consider the following program and predict the output: class Test { public static…
Frank Brosnan
  • 231
  • 1
  • 2
  • 8
12
votes
6 answers

Slight confusion regarding overriding where variables are concerned

I'm preparing for the SCJP (recently rebranded as OCPJP by Oracle) and one particular question that I got wrong on a mock exam has confused me, the answer description doesn't explain things clear enough. This is the question : class A { int x =…
Jimmy
  • 16,123
  • 39
  • 133
  • 213
10
votes
2 answers

How does method chaining work in Java 8 Comparator?

I'm prepping up for the Java 8 certificate and the following has me puzzled a littlebit, maybe someone can help me with this? In the example, a Squirrel class is modelled. It has a name and a weight. Now you can make a Comparator class to sort this…
Cesar
  • 199
  • 13
9
votes
2 answers

java == for Integer

Possible Duplicate: Inconsistent behavior on java's == Integer wrapper objects share the same instances only within the value 127? I have found the following == behaviour for Integer objects and I fail to understand it. (I am well aware that one…
user998692
  • 5,172
  • 7
  • 40
  • 63
9
votes
2 answers

Confused over initialisation of instance variables

I'm studying up for the SCJP exam, upon doing some mock tests I came across this one : It asks what is the output of the following : class TestClass { int i = getInt(); int k = 20; public int getInt() { return k+1; } public static void…
Jimmy
  • 16,123
  • 39
  • 133
  • 213
8
votes
7 answers

Post and Pre increment operators

When i run the following example i get the output 0,2,1 class ZiggyTest2{ static int f1(int i) { System.out.print(i + ","); return 0; } public static void main(String[] args) { …
ziggy
  • 15,677
  • 67
  • 194
  • 287
1
2 3 4 5 6