Java Programming Competition I

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Myrrh
M
Myrrh
Community Contributor
Quizzes Created: 1 | Total Attempts: 1,728
Questions: 50 | Attempts: 1,728

SettingsSettingsSettings
Java Programming Competition I - Quiz

.


Questions and Answers
  • 1. 

    What is the range of an int data type in Java?

    • A.

      –2^15 to 2^15 – 1

    • B.

      0 to 2^15

    • C.

      –2^31 to 2^31 – 1

    • D.

      0 to 2^31

    Correct Answer
    C. –2^31 to 2^31 – 1
    Explanation
    The correct answer is -2^31 to 2^31 - 1. In Java, the int data type is a 32-bit signed two's complement integer. The range of values that can be stored in an int variable is from -2^31 to 2^31 - 1. The lower limit is -2^31 because one bit is used to represent the sign of the number. The upper limit is 2^31 - 1 because the remaining 31 bits are used to represent the magnitude of the number.

    Rate this question:

  • 2. 

    What is the meaning of variable++   ? 

    • A.

      Add one to the variable.

    • B.

      Add one to the variable after its current value has been used.

    • C.

      Add one to the variable before using its value.

    • D.

      Double the value in the variable.

    Correct Answer
    B. Add one to the variable after its current value has been used.
    Explanation
    The expression variable++ is a post-increment operator. It adds one to the variable after its current value has been used. This means that if the variable has a value of 5, for example, the expression variable++ would use the current value of 5 and then increment the variable to 6.

    Rate this question:

  • 3. 

    Consider the following line of Java code.    int x = 1+7*2; What will be the value of x at the end of execution of the line of code? 

    • A.

      9

    • B.

      15

    • C.

      16

    • D.

      21

    Correct Answer
    B. 15
    Explanation
    The value of x at the end of the execution of the line of code will be 15. This is because the multiplication operation has higher precedence than addition, so 7*2 is evaluated first, resulting in 14. Then, 1 is added to 14, giving a final value of 15 for x.

    Rate this question:

  • 4. 

    The Java expression: !((b != 0) || (c <= 5)) is equivalent to: 

    • A.

      (!(b = 0)) && (!(c>5))

    • B.

      (b==0) && (c >5)

    • C.

      (b!=0) && (c

    • D.

      !((b 0) && (c

    Correct Answer
    B. (b==0) && (c >5)
    Explanation
    The given Java expression is using the logical NOT operator (!) to negate the result of the logical OR operator (||). This means that the expression will be true only if both conditions inside the parentheses are false.

    In the first option, !(b = 0) is incorrect because it is using the assignment operator (=) instead of the comparison operator (==).

    In the second option, (b==0) && (c >5) is correct because it checks if b is equal to 0 and c is greater than 5.

    The third and fourth options are incomplete and do not provide a valid expression.

    Rate this question:

  • 5. 

    What does the following program output to the monitor:                   int value = 0, count = 1;                  value = count++ ; System.out.println("value: "+ value  + “count: " + count );   

    • A.

      Value: 0 count: 0

    • B.

      Value: 0 count: 1

    • C.

      Value: 1 count: 1

    • D.

      Value: 1 count: 2

    Correct Answer
    D. Value: 1 count: 2
    Explanation
    The program first assigns the value of count to value and then increments the value of count by 1. Therefore, the output will be "value: 1 count: 2".

    Rate this question:

  • 6. 

    What is the output of the following:                 int a = 0, b = 10;                a = --b ; System.out.println("a= " + a + "  b= " + b ); 

    • A.

      A= 9 b=11

    • B.

      A= 10 b= 9

    • C.

      A= 9 b=9

    • D.

      A= 0 b=9

    Correct Answer
    C. A= 9 b=9
    Explanation
    The output of the given code is "a= 9 b=9". This is because the expression "--b" decrements the value of b by 1 before assigning it to a. So, a becomes 9 and b also becomes 9.

    Rate this question:

  • 7. 

    Which of the answers does the same thing as the following: value += sum++ ;  

    • A.

      Value = value + sum; sum = sum + 1;

    • B.

      Sum = sum + 1; value = value + sum;

    • C.

      Value = value + sum;

    • D.

      Value = value + ++sum;

    Correct Answer
    A. Value = value + sum; sum = sum + 1;
    Explanation
    The given expression "value += sum++" is equivalent to "value = value + sum; sum = sum + 1;". The expression "value += sum++" adds the value of "sum" to the value of "value", and then increments the value of "sum" by 1. This is exactly what is happening in the alternative answer "value = value + sum; sum = sum + 1;".

    Rate this question:

  • 8. 

     What is the name for an application that changes a human-readable programming language into a machine-readable language?  

    • A.

      Compiler

    • B.

      Converter

    • C.

      Encoder

    • D.

      Interpreter

    Correct Answer
    A. Compiler
    Explanation
    A compiler is a software application that translates a human-readable programming language into a machine-readable language. It takes the source code written by a programmer and converts it into a binary code that can be executed by a computer. This process involves multiple stages, such as lexical analysis, syntax analysis, and code generation. Ultimately, a compiler enables the computer to understand and execute the instructions written by the programmer.

    Rate this question:

  • 9. 

    What kind of programming language is Java?

    • A.

      An object-oriented programming language

    • B.

      An array programming language

    • C.

      A logic programming language

    • D.

      A database programming language

    Correct Answer
    A. An object-oriented programming language
    Explanation
    Java is classified as an object-oriented programming language because it is designed to organize and structure code around objects, which are instances of classes. It supports the principles of encapsulation, inheritance, and polymorphism, allowing developers to create modular and reusable code. Java also provides features like class hierarchies, interfaces, and dynamic binding, which are key components of object-oriented programming.

    Rate this question:

  • 10. 

    What is the result of evaluating the following expression?     (1/2 + 3.5) * 2.0  

    • A.

      8.0

    • B.

      7.0

    • C.

      6.0

    • D.

      None of the above

    Correct Answer
    A. 8.0
    Explanation
    The expression (1/2 + 3.5) * 2.0 can be evaluated as follows: 1/2 = 0.5 0.5 + 3.5 = 4.0 4.0 * 2.0 = 8.0 Therefore, the correct answer is 8.0.

    Rate this question:

  • 11. 

    The && operator works with which data types?  

    • A.

      Int

    • B.

      Long

    • C.

      Double

    • D.

      Boolean

    Correct Answer
    D. Boolean
    Explanation
    The && operator works with boolean data types. It is a logical operator that returns true if both operands are true, and false otherwise. It cannot be used with other data types such as int, long, or double.

    Rate this question:

  • 12. 

    Which one of the following is NOT a correct variable name? 

    • A.

      2bad

    • B.

      Zero

    • C.

      TheLastValueButOne

    • D.

      Year2000

    Correct Answer
    A. 2bad
  • 13. 

    Does every Java variable use a data type? 

    • A.

      No---only numeric variables use data types.

    • B.

      No---data types are optional.

    • C.

      Yes---all variables are of the same data type.

    • D.

      Yes---each variable must be declared along with its data type.

    Correct Answer
    D. Yes---each variable must be declared along with its data type.
    Explanation
    Every Java variable must be declared along with its data type. This is because Java is a strongly typed language, which means that variables must have a specific type assigned to them before they can be used. This allows the compiler to allocate the appropriate amount of memory for the variable and perform type checking to ensure that the variable is used correctly. Without declaring the data type of a variable, the compiler would not know how to handle it, resulting in a compilation error.

    Rate this question:

  • 14. 

    When you compile a Java program, what are you doing?

    • A.

      Saving it to disk

    • B.

      Converting it into a form the computer can better understand

    • C.

      Adding it to your program collection

    • D.

      Converting it to object file

    Correct Answer
    B. Converting it into a form the computer can better understand
    Explanation
    When you compile a Java program, you are converting it into a form that the computer can better understand. This process involves translating the human-readable code written in Java into machine-readable code, typically in the form of bytecode. The bytecode can then be executed by the Java Virtual Machine (JVM) on any platform, making Java programs highly portable. Compiling is an essential step in the software development process as it checks for syntax errors and produces an executable file that can be run on a computer.

    Rate this question:

  • 15. 

    What is a variable?

    • A.

      Something that wobbles but doesn't fall down.

    • B.

      Text in a program that the compiler ignores.

    • C.

      A place to store information in a program.

    • D.

      An expression use in Java programming.

    Correct Answer
    C. A place to store information in a program.
    Explanation
    A variable is a place to store information in a program. It is used to store and manipulate data during the execution of a program. Variables can hold different types of data such as numbers, text, or boolean values. They allow programmers to dynamically change and access data throughout the program, making it a fundamental concept in programming.

    Rate this question:

  • 16. 

    What is the process of fixing errors called?

    • A.

      Defrosting

    • B.

      Debugging

    • C.

      Decomposing

    • D.

      Compiling

    Correct Answer
    D. Compiling
  • 17. 

    What do you call a group of statements contained with an opening bracket and a closing bracket?

    • A.

      A block statement

    • B.

      Groupware

    • C.

      Bracketed statements

    • D.

      Not in the list provided

    Correct Answer
    A. A block statement
    Explanation
    A block statement refers to a group of statements enclosed within an opening bracket and a closing bracket. It is used to group multiple statements together, allowing them to be treated as a single unit. This can be helpful for organizing and controlling the flow of code execution.

    Rate this question:

  • 18. 

    What is the output of the following program fragment?  for ( int j = 0;  j <  5; j++ ) {        System.out.print( j + " " ); } System.out.println( );  

    • A.

      0 1 2 3 4 5

    • B.

      0 1 2 3 4

    • C.

      0 1 2 3 4 5

    • D.

      J j j j j

    Correct Answer
    B. 0 1 2 3 4
    Explanation
    The program fragment uses a for loop to iterate from 0 to 4 (since j starts at 0 and the condition j < 5 is true). Inside the loop, it prints the value of j followed by a space. After the loop, it prints a new line character. Therefore, the output will be "0 1 2 3 4".

    Rate this question:

  • 19. 

    What is the output of the following code fragment?  for ( int j = 10;  j >  5; j-- ) {      System.out.print( j + " " ); } System.out.println( );

    • A.

      10 11 12 13 14 15

    • B.

      9 8 7 6 5 4 3 2 1 0

    • C.

      10 9 8 7 6 5

    • D.

      10 9 8 7 6

    Correct Answer
    D. 10 9 8 7 6
    Explanation
    The code starts the loop with j = 10 and continues as long as j is greater than 5. In each iteration, it prints the value of j followed by a space. After the loop, it prints a new line. Therefore, the output of the code is "10 9 8 7 6".

    Rate this question:

  • 20. 

    What must the test be so that the following fragment prints out the integers from 5 through 15?  for ( int j = 5;  ________ ; j++ ){     System.out.print( j + " " ); }System.out.println( );

    • A.

      J<15

    • B.

      J<=16

    • C.

      J<16

    • D.

      J==15

    Correct Answer
    C. J<16
    Explanation
    The test must be "j <= 15" in order for the fragment to print out the integers from 5 through 15. This is because the loop will continue as long as the condition "j <= 15" is true, and it will stop when j becomes greater than 15.

    Rate this question:

  • 21. 

    What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10?  for ( int j = 0; j <= 10; _______   )     System.out.print( j + " " ); System.out.println( );

    • A.

      J+2

    • B.

      J = j+2

    • C.

      J++++

    • D.

      ++j++

    Correct Answer
    B. J = j+2
    Explanation
    The change that needs to be made is to replace the blank with "j = j+2". This will increment the value of j by 2 in each iteration of the loop, resulting in the printing of the even integers 0 2 4 6 8 10.

    Rate this question:

  • 22. 

    What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ?  for ( _______; j < 0; j++    )   System.out.print( j + " " );   System.out.println( );

    • A.

      int j = 0

    • B.

      Int j < 0

    • C.

      Int j = -3

    • D.

      int j = -4

    Correct Answer
    C. Int j = -3
    Explanation
    The correct answer is "int j = -3". This is because the for loop condition is "j < 0", which means the loop will continue as long as j is less than 0. To print out the integers -3, -2, -1, the loop needs to start with j being equal to -3.

    Rate this question:

  • 23. 

    What is the output of the following code fragment?  for ( int j = 5;  j > -5; j-- )   System.out.print( j + " " );     System.out.println( );  

    • A.

      -5 -4 -3 -2 -1 0

    • B.

      5 4 3 2 1 0

    • C.

      5 4 3 2 1 0 -1 -2 -3 -4 -5

    • D.

      5 4 3 2 1 0 -1 -2 -3 -4

    Correct Answer
    D. 5 4 3 2 1 0 -1 -2 -3 -4
    Explanation
    The code starts with the variable j initialized to 5. It then enters a loop that continues as long as j is greater than -5. In each iteration of the loop, it prints the value of j followed by a space. After the loop, it prints a new line.

    So, the output of the code will be: 5 4 3 2 1 0 -1 -2 -3 -4.

    Rate this question:

  • 24. 

     What is the output of the following code fragment?  int count = 0; for ( ;  count < 9; ++count )          System.out.print( count + " " );

    • A.

      0 1 2 3 4 5 6 7 8

    • B.

      Nothing --- the program will not compile.

    • C.

      0 1 2 3 4 5 6 7

    • D.

      1 2 3 4 5 6 7 8 9

    Correct Answer
    A. 0 1 2 3 4 5 6 7 8
    Explanation
    The code fragment initializes a variable "count" to 0. It then enters a for loop that continues as long as "count" is less than 9. In each iteration of the loop, "count" is incremented by 1 and the value of "count" is printed. This process continues until "count" reaches 9, at which point the loop exits. Therefore, the output of the code fragment will be the numbers 0 through 8, separated by spaces.

    Rate this question:

  • 25. 

    What does this code print on the monitor?         int count = 7;                                          while ( count >= 4 )  {            System.out.print( count + " " );  count = count - 1;       } System.out.println(  );  

    • A.

      1 2 3 4 5 6 7

    • B.

      7 6 5 4

    • C.

      6 5 4 3

    • D.

      7 6 5 4 3

    Correct Answer
    B. 7 6 5 4
    Explanation
    The code initializes a variable called "count" with a value of 7. Then, it enters a while loop that continues as long as the value of "count" is greater than or equal to 4. Inside the loop, it prints the value of "count" followed by a space, and then decreases the value of "count" by 1. After the loop, it prints a new line character.

    Therefore, the code will print the numbers 7, 6, 5, and 4 on the monitor, separated by spaces.

    Rate this question:

  • 26. 

    What does this code print on the monitor?        int count =  1;                                          while ( count < 5 )  {             System.out.print( count + " " );       } System.out.println(  );

    • A.

      1 2 3 4

    • B.

      1 2 3 4 5

    • C.

      2 3 4

    • D.

      1 1 1 1 1 1 1 1 1 1 1 . . . .

    Correct Answer
    D. 1 1 1 1 1 1 1 1 1 1 1 . . . .
    Explanation
    The code initializes a variable "count" with the value 1. Then, it enters a while loop that will continue as long as "count" is less than 5. Inside the loop, it prints the value of "count" followed by a space. Since the loop does not modify the value of "count", it will always be 1. Therefore, the code will keep printing "1 " indefinitely until it is terminated.

    Rate this question:

  • 27. 

    What condition should be used so that the code produce this output:         1 2 3 4 5 6 7 8  int count =  1;                                          while (  ___________ )  {             System.out.print( count + " " );                count = count + 1;       }  

    • A.

      Count < 8

    • B.

      Count < 9

    • C.

      Count+1

    • D.

      Count != 8

    Correct Answer
    B. Count < 9
    Explanation
    The condition "count < 9" should be used so that the code produces the output "1 2 3 4 5 6 7 8". This condition ensures that the loop will continue executing as long as the value of count is less than 9. Once count reaches 9, the condition will evaluate to false and the loop will terminate.

    Rate this question:

  • 28. 

    Examine the following code fragment:        int j = 1;       while (  j < 10  ) {                 System.out.println( j + " " );            j = j + j%3;       } What is output?  

    • A.

      1 4 7

    • B.

      1 4 7 10

    • C.

      1 2 5 8

    • D.

      1 2 4 5 7 8

    Correct Answer
    D. 1 2 4 5 7 8
    Explanation
    The code fragment starts with initializing the variable j to 1. Then, it enters a while loop with the condition that j is less than 10. Inside the loop, it prints the value of j followed by a space. After that, it updates the value of j by adding the remainder of j divided by 3 to itself. This process continues until j becomes greater than or equal to 10.

    Based on this logic, the output will be 1 2 4 5 7 8.

    Rate this question:

  • 29. 

    Fill in the blank so that the following adds up the odd numbers from 1 to 99  int sum = 0; for ( int num = 1; num <=99; __________ )   sum += num;        System.out.println( sum );     

    • A.

      J++

    • B.

      Num+2

    • C.

      Num+=2

    • D.

      Num--

    Correct Answer
    C. Num+=2
    Explanation
    The correct answer is "num+=2". This is because "num+=2" is equivalent to "num = num + 2", which means that the variable "num" will increase by 2 in each iteration of the loop. By starting at 1 and increasing by 2, the loop will only consider odd numbers. Therefore, the sum variable will add up all the odd numbers from 1 to 99.

    Rate this question:

  • 30. 

    Another word for "looping" is:

    • A.

      Recapitulation

    • B.

      Tintinabulation

    • C.

      Iteration

    • D.

      Reiteration

    Correct Answer
    C. Iteration
    Explanation
    The word "looping" refers to the process of repeating a set of instructions or actions. Similarly, the word "iteration" also means the repetition of a process or set of instructions. Therefore, "iteration" is another word for "looping".

    Rate this question:

  • 31. 

     For the code below: Which are printed to standard output? int m = 0; while( m++ < 2 ) System.out.print(m);  

    • A.

      0 1

    • B.

      1 2

    • C.

      0 1 2

    • D.

      1 2 3

    • E.

      Nothing and an exception is thrown

    Correct Answer
    B. 1 2
    Explanation
    The code initializes a variable "m" to 0 and then enters a while loop. In each iteration of the loop, the value of "m" is incremented by 1 and then printed to the standard output. The loop continues until "m" becomes 2. Therefore, the numbers 1 and 2 are printed to the standard output.

    Rate this question:

  • 32. 

    What must the test be so that the following fragment prints out the integers 5 through and including 15?  for ( int j = 5;  ________ ; j++ ) {     System.out.print( j + " " ); }     

    • A.

      J

    • B.

      j

    • C.

      J

    • D.

      J==15

    Correct Answer
    C. J
    Explanation
    The test in the blank must be "j

    Rate this question:

  • 33. 

    What must the change be so that the following fragment prints out the even integers 0 2 4 6 8 10?  for ( int j = 0; j <= 10; _______   )   System.out.print( j + " " );

    • A.

      j+2

    • B.

      J = j+2

    • C.

      J++++

    • D.

      ++j++

    Correct Answer
    B. J = j+2
    Explanation
    The correct answer is "j = j+2". This change will increment the value of j by 2 in each iteration of the loop, starting from 0. As a result, the loop will print out the even integers 0, 2, 4, 6, 8, and 10.

    Rate this question:

  • 34. 

    What must the initialization be so that the following fragment prints out the integers -3 -2 -1 ?  for ( _______; j < 0; j++    )   System.out.print( j + " " );

    • A.

      Int j = 0

    • B.

      Int j < 0

    • C.

      Int j = -3

    • D.

      Int j = -4

    Correct Answer
    C. Int j = -3
    Explanation
    The correct answer is "int j = -3" because the initialization of the variable "j" needs to be set to -3 in order for the condition "j < 0" to be true. This will allow the for loop to execute and print out the integers -3, -2, and -1.

    Rate this question:

  • 35. 

    Pick the for loop which duplicates this while loop:  int x = 0; while ( x < 500 ) {   System.out.println( x );   x = x + 5; }  

    • A.

      for ( int x = 0; x < 500; x+=5 ) System.out.println( x );

    • B.

      For ( float x = 0.0; x < 500.0; x += 5.0 ) System.out.println( x );

    • C.

      For ( int x = 500; x >= 0; x-=5 ) System.out.println( x );

    • D.

      for ( int x = 500; x

    Correct Answer
    A. for ( int x = 0; x < 500; x+=5 ) System.out.println( x );
    Explanation
    The correct answer is "for ( int x = 0; x < 500; x+=5 )
    System.out.println( x )". This for loop has the same initialization, condition, and increment as the while loop. It starts with x = 0, continues as long as x is less than 500, and increments x by 5 in each iteration. Additionally, it prints the value of x in each iteration, just like the while loop. Therefore, this for loop duplicates the behavior of the given while loop.

    Rate this question:

  • 36. 

    Pick the for loop which duplicates this while loop:  double x = 0.0; while ( x < 100.0 ) {   System.out.println( x );           x += 0.1;  }

    • A.

      For ( int x = 0; x

    • B.

      For ( int x = 0.0; x < 100.0; x += 0.1 ) System.out.println( x );

    • C.

      Double x; for ( x = 0.0; x < 100.0; x += 0.1 ) System.out.println( x );

    • D.

      For ( double x = 0.0; x < 100.0; ){ x += 0.1 ; System.out.println( x ); }

    Correct Answer
    C. Double x; for ( x = 0.0; x < 100.0; x += 0.1 ) System.out.println( x );
    Explanation
    The correct answer is "double x;

    for ( x = 0.0; x < 100.0; x += 0.1 )
    System.out.println( x );"

    This for loop duplicates the while loop because it initializes a variable x as a double with a value of 0.0. It then sets the condition for the loop to continue as long as x is less than 100.0. After each iteration, it increments x by 0.1. Finally, it prints the value of x. This is exactly what the while loop does.

    Rate this question:

  • 37. 

    What must the test be so that the following fragment prints out the integers -5 through and including 5?  for ( int j = -5;  ________ ; j++ ){   System.out.print( j + " " ); }   

    • A.

      J

    • B.

      J

    • C.

      J>5

    • D.

      J==5

    Correct Answer
    B. J
    Explanation
    The test must be "j

    Rate this question:

  • 38. 

    Fill the blank so that the following fragment prints out 0.2, 0.4, 0.6, 0.8, 1.0,  for ( int j = 2; j <= 10; j+=2   )   System.out.print( __________ + ", " );  

    • A.

      J/10

    • B.

      j%10

    • C.

      (j+1.0)/10

    • D.

      j/10.0

    Correct Answer
    D. j/10.0
    Explanation
    The correct answer is "j/10.0" because it divides the value of j by 10.0, which is a float, resulting in a decimal value. This will print out the desired sequence of 0.2, 0.4, 0.6, 0.8, and 1.0.

    Rate this question:

  • 39. 

    What does the following statement do?     String[] widget;

    • A.

      It declares an array of references to widget objects named String.

    • B.

      It creates a String object named widget.

    • C.

      It creates an array of length zero named widget.

    • D.

      It declares a variable widget which may in the future hold a reference to an array of references to String objects but is initialized to null.

    Correct Answer
    D. It declares a variable widget which may in the future hold a reference to an array of references to String objects but is initialized to null.
    Explanation
    The statement "String[] widget;" declares a variable named widget which is of type String array. It indicates that the variable widget can potentially hold a reference to an array of references to String objects. However, at the moment of declaration, the variable is initialized to null, meaning it does not currently refer to any object or array.

    Rate this question:

  • 40. 

    What is the difference between   String rats; and   String[] rats;                 ?    

    • A.

      There is no difference; both declare rats to be a reference variable.

    • B.

      The first declares rats to be a reference to a String object, the second declares rats to be a reference to an array of String references.

    • C.

      The first constructs a single String object; the second constructs an array of String objects.

    • D.

      The first initializes rats to null; the second initializes rats to an array of nulls.

    Correct Answer
    B. The first declares rats to be a reference to a String object, the second declares rats to be a reference to an array of String references.
    Explanation
    The first statement "String rats;" declares "rats" to be a reference variable that can hold a reference to a single String object. On the other hand, the second statement "String[] rats;" declares "rats" to be a reference variable that can hold a reference to an array of String references. This means that the second statement allows "rats" to refer to multiple String objects, while the first statement only allows it to refer to a single String object.

    Rate this question:

  • 41. 

    What does the following statement do?   int[] values = new int[10] ;  

    • A.

      It declares values to be a reference to an array object and constructs an array object containing 10 integers which are initialized to zero.

    • B.

      It declares values to be a reference to an array object, but initializes it to null.

    • C.

      It declares values to be a reference to an array object which does not yet exist, but will contain 10 zeros when it does.

    • D.

      It declares values to be a reference to an array which contains 10 references to int variables.

    Correct Answer
    A. It declares values to be a reference to an array object and constructs an array object containing 10 integers which are initialized to zero.
    Explanation
    The given statement declares the variable "values" to be a reference to an array object. It also constructs an array object with a size of 10, where each element is initialized to zero.

    Rate this question:

  • 42. 

    What does the following statement do?   String[] names = new String[10] ;  

    • A.

      It declares names to be 10 String objects.

    • B.

      It declares names to be a reference to an array of String references and constructs an array object which can contain references to 10 String objects.

    • C.

      It declares names to be a reference to an array of String references and constructs an array object which contains references to the 10 String objects which it also constructs.

    • D.

      It declares names to be a reference to an array of String references and constructs an array object which contains "10" in its first slot.

    Correct Answer
    B. It declares names to be a reference to an array of String references and constructs an array object which can contain references to 10 String objects.
    Explanation
    The given statement declares the variable "names" to be a reference to an array of String references. It also constructs an array object that can hold references to 10 String objects. This means that the "names" array can store up to 10 String values.

    Rate this question:

  • 43. 

    Given the declaration   String[] names = new String[10] ; Which of the following statements puts a reference to the String "Hello" in the last slot of the array?  

    • A.

      Names[0] = "Hello" ;

    • B.

      Names[10] = "Hello" ;

    • C.

      Names[9] = "Hello" ;

    • D.

      String[ names.length-1 ] = "Hello" ;

    Correct Answer
    C. Names[9] = "Hello" ;
    Explanation
    The correct answer is names[9] = "Hello"; because arrays in Java are zero-indexed, meaning that the first element is at index 0 and the last element is at index length-1. Therefore, to put a reference to "Hello" in the last slot of the array, we need to access index 9.

    Rate this question:

  • 44. 

    What types of information are arrays best suited for?

    • A.

      Lists

    • B.

      Pairs of related information

    • C.

      Trivia

    Correct Answer
    A. Lists
    Explanation
    Arrays are best suited for storing and organizing lists of related information. Arrays allow for the efficient storage and retrieval of multiple values of the same data type, making them ideal for situations where a collection of data needs to be accessed and manipulated as a whole. Arrays provide a convenient way to keep track of and work with lists of items, such as a list of numbers, names, or any other type of data that needs to be grouped together.

    Rate this question:

  • 45. 

    Which of the following statements are valid array declaration? (A) int number(); (B) float average[]; (C) double[] marks; (D) counter int[];  

    • A.

      B & C

    • B.

      A

    • C.

      A & C

    • D.

      D

    Correct Answer
    A. B & C
    Explanation
    The valid array declarations are (B) float average[] and (C) double[] marks. In (B), an array named "average" of type float is declared. In (C), an array named "marks" of type double is declared using the array syntax. (A) int number() is not a valid array declaration as it uses parentheses instead of square brackets. (D) counter int[] is also not a valid array declaration as the type "counter" should come before the variable name.

    Rate this question:

  • 46. 

    46. Consider the following code int number[] = new int[5]; After execution of this statement, which of the following are true?  (A) number[0] is undefined (B) number[5] is undefined (C) number[4] is null (D) number[2] is 0 (E) number.length() is 5  

    • A.

      B, D & E

    • B.

      A & E

    • C.

      C & E

    • D.

      E

    Correct Answer
    A. B, D & E
    Explanation
    After executing the statement "int number[] = new int[5];", the following statements are true:

    (B) number[5] is undefined: This is true because the array "number" has a size of 5, but the indexes of an array start from 0. Therefore, the index number[5] is out of bounds and undefined.

    (D) number[2] is 0: This is true because when an array of integers is created, all the elements are initialized to their default value, which is 0 for integers. Therefore, number[2] will be 0.

    (E) number.length() is 5: This is true because the length property of an array returns the number of elements in the array, which in this case is 5.

    Rate this question:

  • 47. 

    47. Which of the following statements does NOT define a String array called myArray that contains five elements?

    • A.

      String[] myArray = new String[5];

    • B.

      String [] myArray = new String[5];

    • C.

      String []myArray = new String[5];

    • D.

      String[5] myArray = new String();

    Correct Answer
    D. String[5] myArray = new String();
  • 48. 

    48. Which of the following declares an array of int named img?

    • A.

      Int img;

    • B.

      Int[] img;

    • C.

      New int img[];

    • D.

      Int img = int[];

    Correct Answer
    B. Int[] img;
    Explanation
    The correct answer is "int[] img;". This declares an array of int named "img". The square brackets "[]" after the data type "int" indicate that it is an array. The variable name "img" follows the data type and is used to reference the array.

    Rate this question:

  • 49. 

    49. What are the legal indexes for the array ar, given the following declaration:   int[] ar = {2, 4, 6, 8 }    

    • A.

      0, 1, 2, 3

    • B.

      1, 2, 3, 4

    • C.

      2, 4, 6, 8

    • D.

      0, 2, 4. 6

    Correct Answer
    A. 0, 1, 2, 3
    Explanation
    The legal indexes for the array ar are 0, 1, 2, and 3. This is because the array has 4 elements and the indexes start from 0 and go up to 3.

    Rate this question:

  • 50. 

    What is the output of the following code fragment:   int[] ar = {2, 4, 6, 8 }; System.out.println( ar[0] + " " + ar[1] );  

    • A.

      2 6

    • B.

      8

    • C.

      2 4

    • D.

      6 8

    Correct Answer
    C. 2 4
    Explanation
    The code fragment creates an array called "ar" with four elements: 2, 4, 6, and 8. The code then prints the value of the first element (ar[0]) followed by a space and the value of the second element (ar[1]). Therefore, the output of the code is "2 4".

    Rate this question:

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Jul 04, 2024
    Quiz Edited by
    ProProfs Editorial Team
  • Dec 08, 2011
    Quiz Created by
    Myrrh
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.