Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Quiz About Java Arrays

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Quiz About Java Arrays

Uploaded by

mostafa nasser
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Java Arrays Trending in News V

105 Funny Things to Do to M


Question 1 Someone Laugh
Best PS5 SSDs in 2024: Top P
for Expanding Your Storage
Best Nintendo Switch Contr
C in 2024
#geekstreak2024 – 21 Days P
class Test {
Challenge Powered By Deut
public static void main(String args[]) { Bank
int arr[2]; Full Stack Developer Roadm
System.out.println(arr[0]);
[2024 Updated]
System.out.println(arr[1]);
}
}

0
0

garbage value
garbage value

Compiler Error

Exception

Discuss it

Question 1 ‒ Explanation
In Java, it is not allowed to put the size of the array in the declaration because an array declarat
ion specifies only the element type and the variable name. The size is specified when you alloca
te space for the array. Even the following simple program won\'t compile.

class Test {
public static void main(String args[]) {
int arr[5]; //Error
}
}

Question 2

Output of following Java program?

Java

import java.util.Arrays;
class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (Arrays.equals(arr1, arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}

Same

Not Same

Discuss it

Question 2 ‒ Explanation
See http://www.geeksforgeeks.org/compare-two-arrays-java/

Question 3

Consider the following C program which is supposed to compute the transpose of a given 4 x 4
matrix M. Note that, there is an X in the program which indicates some missing statements. Choose
the correct option to replace X in the program.

#include<stdio.h>
#define ROW 4
#define COL 4
int M[ROW][COL] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
main()
{
int i, j, t;
for (i = 0; i < 4; ++i)
{
X
}
for (1 = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
printf ("%d", M[i][j]);
}

for(j = 0; j < 4; ++j){


t = M[i][j];
M[i][j] = M[j][i];
A) M[j][i] = t;
}

for(j = 0; j < 4; ++j){


M[i][j] = t;
t = M[j][i];
B) M[j][i] = M[i][j];
}

C) for(j = i; j < 4; ++j){


t = M[i][j];
M[i][j] = M[j][i];
M[j][i] = t;
}

for(j = i; j < 4; ++j){


M[i][j] = t;
t = M[j][i];
D) M[j][i] = M[i][j];
}

Discuss it

Question 3 ‒ Explanation
To compute transpose j needs to be started with i,so A and B are WRONG
In D, given statement is wrong as temporary variable t needs to be assigned some value and N
OT vice versa

M[i][j] = t;

So the answer is C Check out the correct option C at Solution: http://code.geeksforgeeks.org/r


7wbP6

Question 4

Output of following Java program?

class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1 == arr2)
System.out.println("Same");
else
System.out.println("Not same");
}
}

Same

Not Same

Discuss it

Question 4 ‒ Explanation
See http://www.geeksforgeeks.org/compare-two-arrays-java/

Question 5

Java

class Test {
public static void main(String args[]) {
int arr[] = new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}

0
0

garbage value
garbage value

Compiler Error

Exception

Discuss it

Question 5 ‒ Explanation
Java arrays are first class objects and all members of objects are initialized with default values li
ke o, null.

Question 6

Java

class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}

Same

Not same
Discuss it

Question 6 ‒ Explanation
arr1.equals(arr2) is same as (arr1 == arr2)

Question 7

Predict the output?

Java

// file name: Main.java


public class Main {
public static void main(String args[]) {
int arr[] = {10, 20, 30, 40, 50};
for(int i=0; i < arr.length; i++)
{
System.out.print(" " + arr[i]);
}
}
}

10 20 30 40 50

Compiler Error

10 20 30 40

Discuss it

Question 7 ‒ Explanation
It is a simple program where an array is first created then traversed. The important thing to not
e is, unlike C++, arrays are first class objects in Java. For example, in the following program, siz
e of array is accessed using length which is a member of arr[] object.

Question 8

Which of the following is FALSE about arrays in Java?

A java array is always an object

Length of array can be changed after creation of array

Arrays in Java are always allocated on heap

Discuss it

Question 8 ‒ Explanation
In Java, arrays are objects, they have members like length. The length member is final and cannot
be changed. All objects are allocated on heap in Java, so arrays are also allocated on heap.
Question 9

public class Main {


public static void main(String args[]) {
int arr[][] = new int[4][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
arr[3] = new int[4];

int i, j, k = 0;
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
arr[i][j] = k;
k++;
}
}
for (i = 0; i < 4; i++) {
for (j = 0; j < i + 1; j++) {
System.out.print(" " + arr[i][j]);
k++;
}
System.out.println();
}
}
}

Compiler Error

0
1 2
3 4 5
6 7 8 9

0
0 0
0 0 0
0 0 0 0

9
7 8
4 5 6
0 1 2 3

Discuss it

Question 9 ‒ Explanation
In Java, we can create jagged arrays. Refer Jagged Array in Java for details.

You have completed 9/9 questions .


Your accuracy is 44%.

Last Updated : Mar 22, 2024


Take a part in the ongoing discussion View All Discussion

Company Languages DSA Data Web Python


About Us Python Data Science & Technologies Tutorial
Corporate & Communications Legal Java Structures
Address:- A-143, 9th Floor, In Media
ML HTML Python
C++ Algorithms CSS Programming
Sovereign Corporate Tower, Data Science
Sector- 136, Noida, Uttar Pradesh Contact Us PHP DSA for JavaScript Examples
With Python
(201305) | Registered Address:- K Advertise with GoLang Beginners TypeScript Python Projects
061, Tower K, Gulshan Vivante Data Science
us SQL Basic DSA ReactJS Python Tkinter
Apartment, Sector 137, Noida, For Beginner
GFG Corporate R Language Problems NextJS Web Scraping
Gautam Buddh Nagar, Uttar Solution
Machine
Pradesh, 201305 Android DSA Bootstrap OpenCV Tutorial
Learning
Placement Tutorial Roadmap Web Design Python Interview
ML Maths
Training Tutorials Top 100 Question
Data
Program Archive DSA Django
Visualisation
GeeksforGeeks Interview
Pandas
Community Problems
NumPy
DSA
NLP
Roadmap
Deep Learning
by
Sandeep
Jain
All Cheat
Sheets

Computer DevOps System Inteview School GeeksforGeeks


Science Git Design Preparation Subjects Videos
Operating Linux High Level Competitive Mathematics DSA
Systems AWS Design Programming Physics Python
Computer Docker Low Level Top DS or Algo Chemistry Java
Network Kubernetes Design for CP Biology C++
Database Azure UML Company-Wise Social Science Web Development
Management GCP Diagrams Recruitment English Data Science
System DevOps Interview Process Grammar CS Subjects
Software Roadmap Guide Company-Wise Commerce
Engineering Design Preparation World GK
Digital Logic Patterns Aptitude
Design OOAD Preparation
Engineering System Puzzles
Maths Design
Software Bootcamp
Development Interview
Software Questions
Testing

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


Tutorials
DSA
Data Science Sign In
Web Tech
Courses
Trending Now Data Structures Algorithms System Design Foundational Courses Data Science Practice Problem Python Machine Learning Dat

You might also like