java question 1

2007-07-20 10:27 pm
case 'A' : mark = 90;
case 'B' : mark = 80;
case 'C' : mark = 70;
case 'D' : mark = 60;
break;
case 'E' : mark = 50;
break;
default : mark = 40;
What is the value of mark if the value grade =' c'
Which is correct declares an integer array named scores
a int score;
b int [] scores;
c new int scores [];
d int scores = int[];

回答 (1)

2007-07-22 1:03 pm
✔ 最佳答案
mark would be 60, if ur input is 'C' (upper case)
mark would be 40, if ur input is 'c' (lower case)

Here is the flow....when the code runs to the switch(grade) statement (I assume u omitted it in the question)...it would find the corresponding tag, which is case 'C', mark would be assigned as 70. However, since there's no break statement followed by case 'C', the code would just run (fall thru to) the next statement, which is mark = 60 in case 'D', then it would hit the break statement and exit the switch block.

For the 2nd part of the question...I assume you are asking for a correct integer array declaration

a. is not correct since its not an array
b. is a valid integer array declaration without initialization (array size undefined)
c. not a valid declaration...new keyword cannot be used this way
d. int scores is not an array...int[] would not initialize it properly anyway

Note that even b. is a valid declaration, you would still need to initialize it (set array size and possibly array contents) before using it...

some valid initializations could be:

scores = new int[100]; //an array of size 100 with all elements set as zeros
scores = new int[] {1, 2, 3}; //an array of size 3 with element values [1, 2, 3]

Hope this helps you understand :o)


收錄日期: 2021-04-23 17:09:30
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070720000051KK02227

檢視 Wayback Machine 備份