About Question enthuware.ocpjp.v7.2.1384 :

Moderator: admin

Post Reply
MarcolinoPT
Posts: 1
Joined: Mon Jan 25, 2016 7:27 am
Contact:

About Question enthuware.ocpjp.v7.2.1384 :

Post by MarcolinoPT » Thu Feb 18, 2016 10:22 am

The explanation for this question:

Here assert it being used as an identifier (a method name is also an identifier). However, beginning Java 1.4 assert is a keyword. Therefore, to use 'assert' as an identifier, you have to tell the compile that your code is 1.3 compliant. It will generate a warning but it will compile.

Remember that you CANNOT use 'assert' as a keyword as well as an identifier at the same time.

The explanation for a similar question:

From Java 1.5 onwards, assert is treated as a keyword by default (as opposed to Java 1.4 version where it is treated as an identifier) so there is no need for -source 1.x switch. It is valid though.

Aren't both answers contradicting?

admin
Site Admin
Posts: 7551
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by admin » Thu Feb 18, 2016 11:48 am

Why do you think they are contradictory? In 1.4, assert is a keyword but it is not treated as a keyword by default. It is treated as an identifier. In 1.5, assert is treated as a keyword by default.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by jagoneye » Sun Jan 01, 2017 10:53 am

admin wrote:Why do you think they are contradictory? In 1.4, assert is a keyword but it is not treated as a keyword by default. It is treated as an identifier. In 1.5, assert is treated as a keyword by default.
So how to use assertions in java 1.4 then if it is not treated as keyword by default? :?

admin
Site Admin
Posts: 7551
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by admin » Sun Jan 01, 2017 11:07 am

javac -source 1.4 TestClass.java
If you like our products and services, please help us by posting your review here.

yassine
Posts: 8
Joined: Thu Dec 07, 2017 4:43 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by yassine » Fri Mar 30, 2018 5:09 am

I tried this code in my IDE and the code doesn't compile because assert is a keyword :
Syntax error on token "assert", Identifier expected

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by __JJ__ » Fri Jul 27, 2018 2:28 pm

It can be compiled using all the versions of Java compilers.
This is interesting. I take (or at least, I took, until I read the answer to this question) "versions of Java compilers" to mean, the version of Java under which compilation takes place, not the physical javac.exe file that exists on my hard disk.

If you read it as the latter, then yes, it will compile using any version of javac; if you read it as "the version of java that a compiler assumes when compiling the code", then it will not compile under all versions of such compilers.

If it comes up in the exam, I am grateful for being forewarned.

Touciuciu
Posts: 7
Joined: Fri Aug 17, 2018 8:52 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by Touciuciu » Mon Sep 24, 2018 5:46 am

The answer is wrong, NetBeans says this about the code from this question:
as of release 1.4, 'assert' is a keyword, and may not be used as an identifier
(use -source 1.3 or lower to use 'assert' as an identifier)
Eclipse says this about the code:
Syntax error on token "assert", Identifier expected
Please correct the answer by marking the last choice as the correct one.

admin
Site Admin
Posts: 7551
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1384 :

Post by admin » Mon Sep 24, 2018 5:51 am

Please read the question and the explanation fully and carefully. It is correct.
Also, please use javac command line tool to compile the code instead of relying on IDEs, while preparing for the exam.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests