Java Programming - Language Fundamentals - Discussion

Discussion Forum : Language Fundamentals - General Questions (Q.No. 6)
6.
Which three are legal array declarations?
  1. int [] myScores [];
  2. char [] myChars;
  3. int [6] myScores;
  4. Dog myDogs [];
  5. Dog myDogs [7];
1, 2, 4
2, 4, 5
2, 3, 4
All are correct.
Answer: Option
Explanation:

(1), (2), and (4) are legal array declarations. With an array declaration, you can place the brackets to the right or left of the identifier. Option A looks strange, but it's perfectly legal to split the brackets in a multidimensional array, and place them on both sides of the identifier. Although coding this way would only annoy your fellow programmers, for the exam, you need to know it's legal.

(3) and (5) are wrong because you can't declare an array with a size. The size is only needed when the array is actually instantiated (and the JVM needs to know how much space to allocate for the array, based on the type of array and the size).

Discussion:
39 comments Page 1 of 4.

Janhavi said:   2 years ago
As it seems to be class if we see option 4th and 5th carefully, D is capital
First will create a class named Dog.
class Dog{
String name;
int age;
Dog(String name, int age){
this.name=name;
this.age=age;
}}
public class Info{
main(){
Scanner Sc = new Scanner ();
int age= Sc.nextInt();
String name= Sc.nextLine();
Dog D = new Dog(name,age);

If we create an array to store information of numbers of dogs then we can write as;
Dog[] dog = new dog[size];
So, whenever we create any of our own data types we need to create class of it first.
(3)

Ramesh Gupta said:   2 years ago
Here option A declares a variable named myScores as a two-dimensional array of int type. The [][] denotes the dimensions of the array.

Anil said:   2 years ago
Here we have to initialize the sign of an array while declaring.

That's why 3 is wrong.

Amit said:   4 years ago
I think 2 4 5 is valid.
(4)

Madhava said:   4 years ago
Dog is a reference data type.

Kavi said:   5 years ago
Dog is data type or not? Explain.
(1)

Jit said:   7 years ago
4 is valid because Dog is a user data type created by the user.

Arjunkulothungan said:   7 years ago
Can anyone please explain how to create an own datatype?

Haftu M said:   7 years ago
A is correct, according to the fundamental array declaration.

Prameela said:   7 years ago
Can anyone explain the creation of own data type in Java?


Post your comments here:

Your comments will be displayed after verification.