Correct. Answer is C.
In the first 'for', when trying to execute n.substring for the name "Peter", an exception will be thrown. So in the catch block it will print "Invalid Name", in the second 'for' it will print "omas" and the default values of the remaining elements of the pwd array.
It can be tested, iSnover give the source code below.
Correnct is C, there are no comments to make. If you want to test the code:
public class Main {
public static void main(String[] args) {
String names [] = {"Thomas", "Peter", "Joseph"};
String pwd [] = new String [3];
int idx = 0;
try {
for (String n: names) {
pwd [idx] = n.substring(2, 6);
idx++;
}
}
catch (Exception e) {
System.out.println ("Invalid Name");
}
for (String p: pwd) {
System.out.println (p);
}
}
}
upvoted 3 times
...
Log in to ExamTopics
Sign in:
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
somrita90
3 months, 2 weeks agoyanoolthecool
1 week agoVicky_65
8 months, 2 weeks agocarloswork
1 year, 1 month agoiSnover
1 year, 2 months ago