Cocoa Question of the Day?
Cocoa Question of the Day?
- Subject: Cocoa Question of the Day?
- From: "Craig A. Mattocks" <email@hidden>
- Date: Sun, 9 Dec 2001 02:11:26 -0500
Hi List,
Coriolis.com offers a free e-mail subscription to receive a "question of
the day" (QOD) which are designed to prepare you for Sun's Certified Java
Programmer exam. The answer is appended to the bottom of the next day's
QOD. (See sample below.)
Would any Cocoa experts on the list or Apple's iServices be willing to
provide a similar QOD listserv for Cocoa? It would be a great way to learn,
plus it would provide fodder for discussion.
Thank you for your consideration,
Craig
----------------------------------------------------------------------------
Yesterday's Question of the Day and Answer for Sun Certified Java
Programmer
Sep 11 2001
----------------------------------------------------------------------------
Given the following code fragment
switch( x )
{
case 100 :
System.out.println( "One hundred" );break;
case 200 :
System.out.println( "Two hundred" );break;
case 300 :
System.out.println( "Three hundred" );break;
}
choose all of the declarations of x that will not cause a compiler error.
[Check all correct answers]
A) byte x = 100 ;
B) short x = 200 ;
C) int x = 300 ;
D) long x = 400 ;
Correct Answer(s):
B) short x = 200 ;
C) int x = 300 ;
Explanation:
Answers b and c are correct. The type used in the switch statement must
accommodate all of the values in the case statements. For this reason,
both b and c are acceptable. There are two considerations that the
compiler checks; the variable in the switch statement must be capable of
assuming all of the values in the case statements, and it must be
convertible to an int.
Answer a is incorrect; x cannot be a byte type because the case constants
200 and 300 are not compatible. Answer d is incorrect because switch
statements cannot use long values. You would have to have a specific cast:
switch((int)x) before the compiler would accept it.