About Question enthuware.ocpjp.v7.2.1571 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
DesRenthuware
Posts: 32
Joined: Wed Aug 28, 2013 6:12 am
Contact:

About Question enthuware.ocpjp.v7.2.1571 :

Post by DesRenthuware »

Hi

I did this:

public int getLen(List<?> al) {

if( al != null) return al.get(0).toString().length();
else return 0;
}

Another alternative (assuming the class is typed - and which will then allow type checking at compile time):

import java.util.*;

class Sub<T> {
public int getLen(List<T> al) {

if( al != null) return al.get(0).toString().length();
else return 0;
}
}

class SubTest {
public static void main(String args[]) {
List<String> str = new ArrayList<>();
str.add("Hello");

Sub<String> ttt = new Sub<>();
System.out.println("Out " + ttt.getLen(str));
}
}

Cannelids
Posts: 25
Joined: Thu Jun 01, 2017 2:50 pm
Contact:

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

Post by Cannelids »

Gaah, I hate write-in questions: I got this ‘wrong’ by writing “a1” (a-one) instead of “al” (AL). In retrospect I realise the latter is not an unusual identifier as it’s short for ArrayList, but I wasn’t thinking that at the time so didn’t check or copy-paste it. The difference between 1 and lowercase l is almost impossible to discern in the Enthuware font (though not on this forum), so maybe the camelcase aL would be better? (I was one short of a mark I need in an online course because of this :P )

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

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

Post by admin »

I have updated it to accept a1 as correct answer as well.

thank you for your feedback!
Paul.

Cannelids
Posts: 25
Joined: Thu Jun 01, 2017 2:50 pm
Contact:

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

Post by Cannelids »

Thanks :)

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests