I have a test soon and are having troubles with the following questions..

6) A local variable is a:
(A) variable declared in a field (B) public statement of a variable in a method
(C) private statement of a variable (D) variable declared and used within a single method

9) Which of the following statements enables you to loop through the array “int X[5];”

(A) for(int i=0;i<5;i++) Y=X; (B) for int i=1;i<=5;i+=1) Y=X[i-1];
(C) for int x=0;x<5;x=x+1) Y=X[x]; (D) All of the above

14) Which of these is not a primitive data type?

(A) int (B) short
(C) longer (D) char

15) Which of the following statements are correct in terms of coding in JAVA?

(A) x == y; (B) y = x + y;
(C) x = x + -y; (D) All of the above

Recommended Answers

All 5 Replies

Go through your notes, you might find some answers that way. Look at the ones with code logically. Put them into an IDE and see what it gives you. For Question number 9 try using them to loop through the array.

Where did 1)-5) of your homework go :) ?
6 d 9 a 14 c 15 please do your homework yourself.

6) Let’s try an elimination process. I'm not really sure what's meant by field. Most likely it refers to the attributes of a class, then most certainly option A is not valid. To me there is a distinction between global and local variables. If we assume that global variables are accessed by all methods of a class then surely a local variable is only access within a particular method. Thus A is invalid. I feel that the use of the word statement makes B and C invalid. Statements could never really be local anyway, it just doesn't make sense. Thus option D is the most valid and actually agree with what I said earlier. I just wanted you to see why the others were invalid. You really speaking, variables tend to be only accessible with the parenthesis they are declared unless it's a public attribute.

9) Since you have an array that has 5 elements you may only have indices 0..4. Indeed option A provides values 0..4. Option B is syntactically incorrect, if you had for (int i=1;i<=5;i+=1) Y=X[i-1]; then it would be valid. Option C is also syntactically invalid but if you had for (int x=0;x<5;x=x+1) Y=X[x]; then it too would have been valid. If you made a spelling error then option D would have been correct. What's the lesson here? i++, i += 1 and i = i + 1 are all equivalent. Well that and you had to realize i-1 is valid when using <= in option B.

14) Sorry I don't have time to explain, but longer is not a keyword in Java and thus not a primitive data type.

I got to go, hope that helps!

I'm surprised people gave you the answers. The questions are easily answered by looking in any beginner's java book, such as your school text book.

Question #6 can be the easiest to solve by opening up the glossary in your book. For #9, only 1 solution will even compile. Evaluate the brackets and you'll see. #14, again by using your glossary and process of elimination. And #15, think about what each equation is trying to do, then think about whether it seems valid or not. If you're not sure about why one of them is correct or not, then I'd be happy to explain.

I actually believed the guy :icon_redface: Regardless I hope he learnt something.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.