Declarations and Access Control

Q1: Which cause a compiler error?

A int[ ] scores = {3, 5, 7};

B int [ ][ ] scores = {2,7,6}, {9,3,45};

C String cats[ ] = {"Fluffy", "Spot", "Zeus"};

D boolean results[ ] = new boolean [] {true, false, true};

E Integer results[ ] = {new Integer(3), new Integer(5), new Integer(8)};

ANS:B - int [ ][ ] scores = {2,7,6}, {9,3,45};

Option B generates a compiler error: <identifier> expected. The compiler thinks you are trying to create two arrays because there are two array initialisers to the right of the equals, whereas your intention was to create one 3 x 3 two-dimensional array. To correct the problem and make option B compile you need to add an extra pair of curly brackets: int [ ] [ ] scores = { {2,7,6}, {9,3,45} };



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.