1.
Int[] foo= {10, 12, 13, 15, 18};int n, result=0;int main (){for ( n=0 ; n<5 ; ++n )result += foo[n];}}What is the output of this program?
2.
Public class TestArray {
public static void main(String[] args) { double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements for (int i = 0; i < myList.length; i++) { System.out.println(myList[i] + " "); } // Summing all elements double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; } System.out.println("Total is " + total); // Finding the largest element double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; } System.out.println("Max is " + max); }}
What is the output of this source code?
3.
Public static void main(String[] args){ Scanner console = new Scanner(System.in); int n, temp=0; S.O.P("Enter a positive integer: "); n=console.nextInt(); temp = mysteryFunction (n); if(temp==0) S.O.P( n + " is a prime number."); else S.O.P( n +" is not a prime number."); return 0;} public int mysteryFunction(int n){ int i, flag=0; for(i=2;i<=n/2;++i) { if(n%i==0) { flag=1; break; } } return flag;}
What will be the output if the user inputs the value of the ffg as the value of n?
1.) n = 5 2.) n=-2 3.) n = 250 4.) n=05.) n = 10
4.
It is a collection of values with the same data type.
Correct Answer
B. Array
Explanation
An array is a data structure that allows you to store multiple values of the same data type under a single variable name. It provides a way to efficiently manage and access a collection of values. In an array, each value is assigned an index, starting from 0, which allows for easy retrieval and manipulation of specific elements within the collection. Therefore, an array is the correct answer as it fits the description of a collection of values with the same data type.
5.
The number of elements an array can hold depends on
Correct Answer
D. Size of an Array
Explanation
The correct answer is "Size of an Array" because the number of elements an array can hold is determined by its size. The size of an array defines the total amount of memory allocated for storing its elements. Therefore, the larger the size of the array, the more elements it can hold. The type of the array, index of the array, and strength of the array do not directly affect the number of elements an array can hold.
6.
How are arrays identified
Correct Answer
C. By their array names
Explanation
Arrays are identified by their array names because the name of an array is used to access and manipulate its elements. The array name acts as a reference to the memory location where the array is stored. By using the array name, we can easily access and modify the elements of the array. The array size determines the number of elements in the array, while indexes are used to access specific elements within the array. However, the primary identifier for an array is its name.
7.
This tells what kind of values an array can hold
Correct Answer
D. Type of an array
Explanation
The correct answer is "Type of an array" because the type of an array determines the kind of values that can be stored in it. The type of an array specifies the data type of its elements, such as integers, strings, or objects. This is important because it ensures that only compatible values can be assigned to the array, preventing type errors and ensuring data integrity.
8.
What type of values can the array String[] places = new String[20] hold?
Correct Answer
D. String
Explanation
The array "places" is declared as a String array and initialized with a size of 20. This means that each element in the array can hold a value of type String. Therefore, the correct answer is String.
9.
In the array String[] places = new String[20], how many values can be stored in it?
Correct Answer
B. 20
Explanation
The given array declaration "String[] places = new String[20]" creates an array named "places" with a size of 20. This means that the array can store 20 values.
10.
The first index of an array should be always be ________?
Correct Answer
D. 0
Explanation
The first index of an array should always be 0 because in most programming languages, arrays are zero-indexed, meaning the first element is accessed using the index 0. This convention allows for easier and more efficient manipulation of array elements using mathematical operations and simplifies the implementation of algorithms that rely on indexing.
11.
The highest possible index of an array should always be?
Correct Answer
C. The size of the array -1
Explanation
In most programming languages, arrays are zero-indexed, meaning that the first element in the array has an index of 0. Therefore, the highest possible index of an array would be one less than the size of the array. For example, if the size of the array is 4, the highest possible index would be 3.
12.
It is used to access a value in array. In other words, it serves as the address of value in an array
Correct Answer
B. Array index
Explanation
The array index is used to access a specific value in an array. It represents the position or address of the value within the array. By specifying the index, we can retrieve the desired element from the array.
13.
Which of the following is the proper way of assigning values in an array?
Correct Answer
D. Int[] ages = {1.9, 2.9, 3.4, 3.5};;
Explanation
The proper way of assigning values in an array is by using the syntax "int[] ages = {1.9, 2.9, 3.4, 3.5}". This syntax declares an array of integers named "ages" and initializes it with the given values {1.9, 2.9, 3.4, 3.5}. The use of "int[]" indicates that it is an array of integers, and the curly braces {} are used to enclose the values that are being assigned to the array elements. The given option "int[] ages = {1.9, 2.9, 3.4, 3.5}" follows this correct syntax.
14.
It is a kind of array which contains another array(s). In other words, it is an array of arrays.
Correct Answer
D. Multidimensional
Explanation
A multidimensional array is a type of array that contains other arrays. It is used to store data in multiple dimensions, such as rows and columns. This allows for the organization and manipulation of complex data structures. Unlike a one-dimensional array, which only has a single row or column, a multidimensional array can have multiple rows and columns. It provides a way to represent and work with data that has multiple levels of depth and complexity.
15.
It is form of array which can be represented in a tabular format - it has a row and a column
Correct Answer
B. Two-dimensional
Explanation
A two-dimensional array is a form of array that can be represented in a tabular format, with rows and columns. Unlike a one-dimensional array, which is a simple list of elements, a two-dimensional array allows for organizing data in a grid-like structure. It is commonly used to represent tables, matrices, and grids in programming. Each element in a two-dimensional array is accessed using two indices, one for the row and one for the column.
16.
In the array int[][] temp = new int[5][7] how many rows can the array hold?
Correct Answer
B. 5
Explanation
The given array declaration "int[][] temp = new int[5][7]" creates an array called "temp" with 5 rows and 7 columns. Therefore, the array can hold a total of 5 rows.
17.
In the array int[][] temp = new int[5][7] how many columns can the array hold?
Correct Answer
D. 7
Explanation
The array "temp" is declared as int[5][7], which means it is a two-dimensional array with 5 rows and 7 columns. Therefore, the array can hold a maximum of 7 columns.
18.
It is an access modifier that lets the method be seen by other classes and methods
Correct Answer
A. Public
Explanation
Public is an access modifier that allows the method to be visible and accessible to other classes and methods. It is the most permissive access modifier, as it provides the widest scope of visibility. With the public access modifier, the method can be called and used by any other class or method in the program. This allows for easy communication and interaction between different parts of the code.
19.
Say we have a method public double add (double x, double y), what is the correct way to call this method in the main Java method
Correct Answer
C. Add(x,y)
Explanation
The correct way to call the add method in the main Java method is by passing two double values, x and y, as arguments. This is indicated by the option "add(x,y)".
20.
It is an access modifier that lets the method be seen by other classes and methods but with reservations
Correct Answer
D. Protected
Explanation
The correct answer is "Protected". Protected is an access modifier that allows the method to be visible to other classes and methods within the same package or subclass, but not to classes outside of the package. It provides a level of restriction, allowing limited access to the method.
21.
It refers to the type of data returned or accepted by the method
Correct Answer
D. Return type
Explanation
The return type refers to the type of data that is expected to be returned or accepted by the method. It specifies the data type of the value that the method will return to the caller. The return type is defined in the method prototype and is used in the method definition to specify the type of the value that will be returned.
22.
This is used to execute a method. When invoked, the method body will then be executed
Correct Answer
C. Method call
Explanation
A method call is used to execute a method. When a method is invoked or called, the code within the method body will be executed. This allows the program to perform the actions or operations defined within the method. The method call is the statement that triggers the execution of the method and allows the program to utilize the functionality provided by that method.