0% found this document useful (0 votes)
133 views

0 Javamcq-Part1

Private constructors ensure only one instance of a class can exist at any given time by preventing other classes from instantiating that class. Class.getInstance() can create an object of a class even if the class does not have any constructor defined. Constructors can take any number of parameters but have no explicit return type.

Uploaded by

Kenil Togadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views

0 Javamcq-Part1

Private constructors ensure only one instance of a class can exist at any given time by preventing other classes from instantiating that class. Class.getInstance() can create an object of a class even if the class does not have any constructor defined. Constructors can take any number of parameters but have no explicit return type.

Uploaded by

Kenil Togadiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

1-What is true about private constructor?

a) Private constructor ensures only one instance of a class exist at any point of time
b) Private constructor ensures multiple instances of a class exist at any point of time
c) Private constructor eases the instantiation of a class
d) Private constructor allows creating objects in other classes
Answer: a
2-What is false about constructor?
a) Constructors cannot be synchronized in Java
b) Java does not provide default copy constructor
c) Constructor can have a return type
d) “this” and “super” can be used in a constructor
Answer: c
3-What is true about Class.getInstance()?
a) Class.getInstance calls the constructor
b) Class.getInstance is same as new operator
c) Class.getInstance needs to have matching constructor
d) Class.getInstance creates object if class does not have any constructor
Answer: d
4-What is true about constructor?
a) It can contain return type
b) It can take any number of parameters
c) It can have any non access modifiers
d) Constructor cannot throw an exception
Answer: b
5-Abstract class cannot have a constructor.
a) True
b) False
Answer: b
6-What is true about protected constructor?
a) Protected constructor can be called directly
b) Protected constructor can only be called using super()
c) Protected constructor can be used outside package
d) protected constructor can be instantiated even if child is in a different package
Answer: b
7-What would be the behavior if one parameterized constructor is explicitly defined?
a) Compilation error
b) Compilation succeeds
c) Runtime error
d) Compilation succeeds but at the time of creating object using default constructor, it
throws compilation error
Answer: d

8-What would be behaviour if the constructor has a return type?


a) Compilation error
b) Runtime error
c) Compilation and runs successfully
d) Only String return type is allowed
Answer: a
9-What is the return type of Constructors?
a) int
b) float
c) void
d) none of the mentioned
Answer: d
10-Which keyword is used by the method to refer to the object that invoked it?
a) import
b) catch
c) abstract
d) this
Ans: Answer: d

11-What is constructor in java?


A. It is an object of code
B. It is a recursive block of code
C. It is a block of code like method
D. All of above
Answer: C
12- Which of the following statements are incorrect?
a) default constructor is called at the time of object declaration
b) constructor can be parameterized
c) finalize() method is called when a object goes out of scope and is no longer
needed
d) finalize() method must be declared protected
Answer: c
12- What will be the output of the following Java code?
1. class area
2. {
3. int width;
4. int length;
5. int area;
6. void area(int width, int length)
7. {
8. this.width = width;
9. this.length = length;
10. }
11.
12. }
13. class Output
14. {
15. public static void main(String args[])
16. {
17. area obj = new area();
18. obj.area(5 , 6);
19. System.out.println(obj.length + " " + obj.width);
20. }
21. }

a) 0 0
b) 5 6
c) 6 5
d) 5 5
Answer: c
13-A Java constructor is like a method without ___.
A) statements
B) return type
C) argument list
D) None
Ans:B
14-The name of a constructor and the name of a class are ___.
A) Same
B) Different
C) -
D) -
Ans:A
15-The placement of a constructor inside a class should be ___.
A) Always at the beginning of class
B) Always at the end of class
C) Anywhere in the class
D) None
Ans:C
16-The purpose of a Java constructor is ___.
A) Initialization of variables with passed data
B) Writing custom code
C) Accepting other objects as inputs
D) All the above
17-The purpose of a Java constructor is ___.
A) Initialization of variables with passed data
B) Writing custom code
C) Accepting other objects as inputs
D) All the above
Ans:D
18-Memory is allocated to an object once the execution of ___ is over in Java language.
A) main method
B) constructor
C) destructor
D) None
19-Memory is allocated to an object once the execution of ___ is over in Java language.
A) main method
B) constructor
C) destructor
D) None
Ans:B
20-What is the output of the below Java program?
public class TestingConstructor
{
void TestingConstructor()
{
System.out.println("Amsterdam");
}

TestingConstructor()
{
System.out.println("Antarctica");
}

public static void main(String[] args)


{
TestingConstructor tc = new TestingConstructor();
}
}
A) Antarctica
B) Amsterdam
C) No output
D) Compiler error
Ans:A
21-In Java, a constructor with no parameters or no arguments is called ___ constructor.
A) Default constructor
B) User-defined constructor
C) -
D) –
Ans:A
22-In Java, a constructor with one or more arguments or parameters is called a ___ constructor.
A) Default constructor
B) User-defined constructor or Non-default constructor
C) –

D) –

Ans:B
23-The compiler adds a default no-argument constructor to a class if it ___.
A) does not define a constructor at all.
B) defines at least one constructor with arguments
C) -
D) –
Ans:A
24- Overloading of constructors in Java means adding more than ___ constructors with the
different argument list.
A) 1
B) 2
C) 3
D) 8
Ans:A
25-What is the output of the below Java program with constructors?
public class Constructor2
{
int count=10;
Constructor2(int count)
{
System.out.println("Count=" + count);
}

public static void main(String[] args)


{
Constructor2 con = new Constructor2();
}
}
A) Count=0
B) Count=10
C) Compiler error
D) None of the above
Ans:C
27-A constructor can call another overloaded constructor using the ___ keyword in Java.
A) super
B) local
C) con
D) this
Ans:A
28-What is the output of the below Java program with overloaded constructors?
public class Constructor3
{
int birds=10;
Constructor3()
{
this(20);
}
Constructor3(int birds)
{
System.out.println("Birds=" + birds);
}
public static void main(String[] args)
{
Constructor3 con = new Constructor3();
}
}
A) Birds=0
B) Birds=10
C) Birds=20
D) Compiler error
Ans:C
29-In Java, you can pass __ variables from one constructor to another overloaded constructor.
A) local variables
B) static variables
C) non-static variables
D) local and static variable
30- Choose the correct way of calling the second constructor from the first constructor in the
below code options.
A)
Constructor5()
{
int a=30;
this('A');
}
Constructor5(char c)
{
//
}
B)
Constructor5()
{
int a=30;
this('A');
System.out.println("Success");
}
Constructor5(char c)
{
//
}
C)
Constructor5()
{
this('A');
System.out.println("Success");
}
Constructor5(char c)
{
//
}
D) All the above
Ans: C
31-What is the output of the below Java program with many constructors?
public class Constructor7
{
Constructor7(int a)
{
System.out.println("Book=" + a);
}
Constructor7(float a)
{
System.out.println("Pen="+ a );
}
public static void main(String[] args)
{
Constructor7 con = new Constructor7(50.5f);
}
}
Ans:B
32-What is the output of the below Java program with many constructors?
public class Constructor8
{
Constructor8(boolean a)
{
System.out.println("MODEM="+ a );
}
Constructor8(float a)
{
System.out.println("ROUTER=" + a);
}
public static void main(String[] args)
{
Constructor8 con1 = new Constructor8(50);
Constructor8 con2 = new Constructor8(false);
}
}
A)
ROUTER=50.0
MODEM=false
B)
ROUTER=50
MODEM=false
C) Compiler error
D) None

Ans:B
33-Choosing a suitable overloaded constructor happens at ___ time in Java.
A) Compile-time
B) Run time
C) -
D) –
Ans:A
34-Java constructor overloading follows ___ principle in Object-Oriented programming.
A) Inheritance
B) Encapsulation
C) Polymorphism
D) None
Ans:C
35- Java allows calling or invoking a method from a constructor. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) –
Ans : A
36) What is the return type of Constructors?
A.int
B.float
C.void
D.None of the above
Ans:C
1- Which of these class is superclass of every class in Java?
a) String class
b) Object class
c) Abstract class
d) ArrayList class
Ans:b
2-Which of these method of Object class can clone an object?
a) Objectcopy()
b) copy()
c) Object clone()
d) clone()
Answer: c
3-Which of these method of Object class is used to obtain class of an object at run time?
a) get()
b) void getclass()
c) Class getclass()
d) None of the mentioned
Answer: c
4-Which of these keywords can be used to prevent inheritance of a class?
a) super
b) constant
c) class
d) final
Answer: d
5-Which of these keywords cannot be used for a class which has been declared final?
a) abstract
b) extends
c) abstract and extends
d) none of the mentioned
Answer: a
6-What will be the output of the following Java program?

abstract class A
{
int i;
abstract void display();
}
class B extends A
{
int j;
void display()
{
System.out.println(j);
}
}
class Abstract_demo
{
public static void main(String args[])
{
B obj = new B();
obj.j=2;
obj.display();
}
}
a) 0
b) 2
c) Runtime Error
d) Compilation Error
Answer: b
7-What will be the output of the following Java program?

class A
{
int i;
int j;
A()
{
i = 1;
j = 2;
}
}
class Output
{
public static void main(String args[])
{
A obj1 = new A();
A obj2 = new A();
System.out.print(obj1.equals(obj2));
}
}
a) false
b) true
c) 1
d) Compilation Error
Answer: a
8-What will be the output of the following Java code?

class Output
{
public static void main(String args[])
{
Object obj = new Object();
System.out.print(obj.getclass());
}
}
a) Object
b) class Object
c) class java.lang.Object
d) Compilation Error
Answer: c

9-Java is a ___ programming language.


A) Functional
B) Object-Oriented
C) Theoretical
D) All the above
Ans: B

10-In Java programming language, the code is placed inside ___.


A) Classes, Interfaces
B) Methods
C) Blocks
D) All the above
Ans: D

11- Properties are implemented using ___ in Java.


A) Methods
B) Variables
C) Interfaces
D) All the above
Ans: B

12-Which is the file extension used for a public Java class source code?
A) .j
B) .class
C) .java
D) None

Ans: C

13-Which is the file extension used for a compiled Java class file?
A) .j
B) .java
C) .class
D) .cls
Ans: C
14-State TRUE or FALSE.
The source-code of An Abstract-Class or Interface is kept inside a .java file.
A) FALSE
B) TRUE
C) -
D) –
Ans:B
15-After compilation, an Interface or Abstract-Class is kept in a ___ file in Java
programming.
A) .java
B) .cls
C) .class
D) .interface
Ans: C
16-In a .java file, how many numbers of public types namely class, interface or abstract
can be managed?
A) 1
B) 2
C) 3
D) Any number
Ans: A

17-A Java class can contain___.


A) Variables
B) Methods, Constructors
C) Inner Classes (A class inside another class)
D) All the above
Ans: d

18-How many maximum numbers of objects can be created from a single Class in Java?
A) 32
B) 64
C) 256
D) no limit
Ans: D

19-Creating an object from a class is also called ____.


A) Initializing
B) Instantiating
C) Interfacing
D) None of the above.
Ans: B

20-The keyword used to create a new object in Java is ___.


A) class
B) java
C) new
D) create
Ans: C

21-Choose the correct statements about choosing a name for a class in Java.
A) The class name can start with only a letter or underscore or dollar sign.
B) The class name can contain numbers
C) The class name can not start with a number
D) All the above
Ans: D

22-An object is created at __ time in Java.


A) Compile-time
B) Run time
C) Assembling time
D) None of the above
Ans: B

23- Choose the correct statement about Java main method.


A) The main method is not a required method
B) The main method must be declared public static void.
C) you can define program flow using the main method. The Java virtual machine calls
the main method directly.
D) All the above

24- Choose the correct syntax for declaring a Java class below.
A)
class CLASSNAME
{

}
B)
CLASSNAME class
{

}
C)
class CLASSNAME;
{

}
D)
Class CLASSNAME
{

}
Ans: A
25-Choose the correct way of creating an object of the below class.
class Table
{
Table(){System.out.println("Table Created");}
}
A)
Table t = new Table;
B)
Table t = new Table();
C)
Table() t = new Table();
D) None of the above

Ans: B

26- Which of the following option leads to the portability and security of Java?
a. Bytecode is executed by JVM
b. The applet makes the Java code secure and portable
c. Use of exception handling
d. Dynamic binding between objects
Answer: (a)

27-Which of the following is not a Java features?


a. Dynamic
b. Architecture Neutral
c. Use of pointers
d. Object-oriented
Answer: (c)
28-What should be the execution order, if a class has a method, static block, instance
block, and constructor, as shown below?

public class First_C {


public void myMethod()
{
System.out.println("Method");
}

{
System.out.println(" Instance Block");
}

public void First_C()


{
System.out.println("Constructor ");
}
static {
System.out.println("static block");
}
public static void main(String[] args) {
First_C c = new First_C();
c.First_C();
c.myMethod();
}
}
a. Instance block, method, static block, and constructor
b. Method, constructor, instance block, and static block
c. Static block, method, instance block, and constructor
d. Static block, instance block, constructor, and method
Answer: (d)
29-What will be the output of the following program?
public class MyFirst {
public static void main(String[] args) {
MyFirst obj = new MyFirst(n);
}
static int a = 10;
static int n;
int b = 5;
int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
n = 20;
}
// Static Block
static
{
a = 60;
}
}
a. 10, 5, 0, 20, 0
b. 10, 30, 20
c. 60, 5, 0, 20
d. 60, 30, 0, 20, 0
Answer: (d)
30-Which of the following is a valid declaration of a char?
a. char ch = '\utea';
b. char ca = 'tea';
c. char cr = \u0223;
d. char cc = '\itea';
Answer: (a)

31-What is the return type of the hashCode() method in the Object class?
a. Object
b. int
c. long
d. void
Answer: (b)

32-Which of the following is a valid long literal?


a. ABH8097
b. L990023
c. 904423
d. 0xnf029L
Answer: (d)

33-What does the expression float a = 35 / 0 return?


a. 0
b. Not a Number
c. Infinity
d. Run time exception
Answer: (c)

34-Evaluate the following Java expression, if x=3, y=5, and z=10:

++z + y - y + z + x++
a. 24
b. 23
c. 20
d. 25
Answer: (d)
What will be the output of the following program?
public class Test {
public static void main(String[] args) {
int count = 1;
while (count <= 15) {
System.out.println(count % 2 == 1 ? "***" : "+++++");
++count;
} // end while
} // end main
}
a. 15 times ***
b. 15 times +++++
c. 8 times *** and 7 times +++++
d. Both will print only once
Answer: (c)

35-Which of the following tool is used to generate API documentation in HTML format from
doc comments in source code?
a. javap tool
b. javaw command
c. Javadoc tool
d. javah command
Answer: (c)

36-Which of the following creates a List of 3 visible items and multiple selections abled?
a. new List(false, 3)
b. new List(3, true)
c. new List(true, 3)
d. new List(3, false)
Answer: (b)

37-Which of the following for loop declaration is not valid?


a. for ( int i = 99; i >= 0; i / 9 )
b. for ( int i = 7; i <= 77; i += 7 )
c. for ( int i = 20; i >= 2; - -i )
d. for ( int i = 2; i <= 20; i = 2* i )
Answer: (a)
Which method of the Class.class is used to determine the name of a class represented by
the class object as a String?
a. getClass()
b. intern()
c. getName()
d. toString()
Answer: (c)

38-In which process, a local variable has the same name as one of the instance variables?
a. Serialization
b. Variable Shadowing
c. Abstraction
d. Multi-threading
Answer: (b)

39-Which of the following is true about the anonymous inner class?


a. It has only methods
b. Objects can't be created
c. It has a fixed class name
d. It has no class name
Answer: (d)
40-Which package contains the Random class?
a. java.util package
b. java.lang package
c. java.awt package
d. java.io package
Answer: (a)

41-What do you mean by nameless objects?


a. An object created by using the new keyword.
b. An object of a superclass created in the subclass.
c. An object without having any name but having a reference.
d. An object that has no reference.
Answer: (d)

42-Which option is false about the final keyword?


a. A final method cannot be overridden in its subclasses.
b. A final class cannot be extended.
c. A final class cannot extend other classes.
d. A final method can be inherited
Answer: (c)

43-Variable of class type are also referred to as________variables.

a) Orientation

b) Position

c) Reference
d) Indication

Ans:c

44-Operator________allocates the memory for an object and returns the address of the
object for later use.

a) Int

b) Float

c) New

d) Real

Ans: c

45-_________is the address of the memory location where the object is stored.

a) Memory

b) Variable

c) Reference

d) None of these

Ans: c

46-There is a special portion of memory called the______where the objects live.

a) Heap

b) Pile

c) Stack

d) All of these

Ans: a

47-When an object is created, in addition to allocating memory, a special method


called_______is executed to perform initial task.

a) Function
b) Constructor

c) Class

d) Method

Ans: b

48-An object can be created of type Room and assign its address to variable rl as_______

a) rl = new Room();

b) rl = Room() new;

c) rl = Class Room();

d) None of these

Ans: a

49-With empty parentheses without arguments, a ______ Constructor is called.

a) parameterized

b) default

c) null

d) Method

Ans: B

50-Observe the following Room r2 = new RoomO : - Variable r2 contains a______or


address of memory location where a new object is created.

a) Memory

b) Variable

c) Reference

d) None of these

Ans: C

51-The class determines only the_______of the variables.


a) Types

b) Collection

c) Location

d) Set

Ans: A

52-The actual________is contained inside the individual objects and not in the class.

a) Information

b) Data

c) Collection

d) Variables

Ans: b

53-Every______has its own set of data.

a) Class

b) Variable

c) Operator

d) Object

Ans: d

54-_______allocated different memory space to hold their data values.

a) Classes

b) Variables

c) Operators

d) Objects

Ans: d
55-In Java, when______are no more needed, the memory is claimed back for reuse.

a) Classes

b) Variables

c) Operators

d) Objects

Ans: d

56-Java has a garbage collector that looks for unused______and reclaims the memory
that those objects are using.

a) Objects

b) Variables

c) Cells

d) Memory spaces

Ans: a

57-In Java, there is no requirement to do any explicit freeing of______

a) Cells

b) Variables

c) Memory

d) Class

Ans: c

58-In Object-oriented programming (OOP) languages, creating an object is also


called______instantiation.

a) Class

b) Object

c) Inheritance
d) Polymorphism

Ans: b

59-An______for an object is created by allocating memory to store data for that object.

a) Instance

b) Example

c) Illustration

d) None of these

Ans: A

60-An object that belongs to a_____is said to be an instance of that class.

a) Cells

b) Variables

c) Memory

d) Class

Ans: d

61-An________of a class is another word for an actual object.

a) Instance

b) Example

c) Memory

d) None of these

Ans: a

62-_______is an abstract representation of an object, whereas an instance is its concrete


representation.

a) Cells

b) Variables
c) Memory

d) Class

Ans: d

63-The terms instance and object are often used interchangeably in______languages.

a) Programming

b) OOP

c) All

d) None of these

Ans: b

64-Each______of a class can hold different values for its attributes in variables declared
in a class.

a) Instance

b) Example

c) Illustration

d) None of these

Ans: A

65-______variables are created at the time of creating an object and stay throughout the
life of the object.

a) Program

b) Instance

c) Inherited

d) Duplicate

Ans: b

66-______variables define the attributes of an object.


a) Program

b) Instance

c) Inherited

d) Duplicate

Ans: b

67-The______defines the kind of attribute.

a) Program

b) Variables

c) Property

d) Class

Ans: d

68-To define an object's behaviour_______are created.

a) Instances

b) Programs

c) Methods

d) Functions

Ans: c

69-In Java_____can be defined inside a class only.

a) Instances

b) Programs

c) Methods

d) Functions

Ans: c
70-The_______can be invoked using the objects to access or modify the instance
variables.

a) Instances

b) Programs

c) Methods

d) Functions

Ans: c

71-The____invoked using the objects are known as instance methods.

a) Instances

b) Programs

c) Methods

d) Functions

Ans: c

72-Invoking a______is to ask the object to perform some task.

a) Instance

b) Program

c) Method

d) Function

Ans: c

73-The______keyword returns a reference to an object that represents an instance of the


class.

a) new

b) New

c) NEW
d) nEW

Ans: a

74-Instance variables and instance methods are accessed via_______

a) Classes

b) Variables

c) Operators

d) Objects

Ans: d

75-Objects can be referred by using_____operator.

a) Colon (:)

b) Underscore(_)

c) Dot (.)

d) Angle Bracket(<)

Ans: c

76-Associatively of______operator is from left to right.


a) Colon (:)
b) Underscore(_)
c) Dot (.)
d) Angle Bracket(<)
Ans: c

77-When the programmer declares an object_______object is not created.


a) Class
b) Variable
c) Operator
d) Method
Ans: A

78-Reference variable does not refer to any object and its initial value is_____by default.
a) Zero
b) One
c) Null
d) Minus one
Ans: c

79-Referring instance variable or invoking method with null reference will give
an______
a) Example
b) Error
c) Mistake
d) Inaccurate result
Ans: b

80-Protection of data is possible with the use of access______


a) Modifiers
b) Operators
c) Methods
d) None of these
mcq on static keyword and arrays
1.static means
a) class level
b) object level
c) method level
d) function level
ans: a
2.main method is static because to execute main method without creating object by jvm
a)yes
b)no
c)-
d)-
ans: a
3.we can overload static methods
a)true
b)false
c)-
d)-
ans: a
4.we can override static methods
a)yes
b)no
c)-
d)-
ans: b
5.can we write static public void main(String [] args)?
a) yes
b) no
c)-

d)-
ans: a
6. arrays in java are implemented as?
a) class
b) object
c) variable
d) none of the mentioned
answer: b
7-which of these keywords is used to prevent content of a variable from being modified?

a) final
b) last
c) constant
d) static
answer: a
8-which of these cannot be declared static?
a) class
b) object
c) variable
d) method
answer: b

9-which of the following statements are incorrect?


a) static methods can call other static methods only.
b) static methods must only access static data.
c) static methods can not refer to this or super in any way.
d) when object of class is declared, each object contains its own copy of static variables.
answer: d
10-which of the following statements are incorrect?
a) variables declared as final occupy memory.
b) final variable must be initialized at the time of declaration.
c) arrays in java are implemented as an object.
d) all arrays contain an attribute-length which contains the number of elements stored in the
array.
answer: a

11-which of these methods must be made static?


a) main()
b) delete()
c) run()
d) finalize()
answer: a

12-what is the output of this program?


Class access{
public int x;
static int y;
void cal(int a, int b){
x += a ;
y += b;
}
}
Class static_specifier {
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
system.out.println(obj1.x + " " + obj2.y);
}
}
a) 1 2
b) 2 3
c) 3 2
d) 1 5
answer: d
13-what is the output of this program?
Class access{
static int x;
void increment(){
x++;
}
}
Class static_use {
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.increment();
obj2.increment();
system.out.println(obj1.x + " " + obj2.x);
}
}
a) 1 2
b) 1 1
c) 2 2
d) compilation error
answer: c

14-what is the output of this program?


Class static_out {
static int x;
static int y;
void add(int a , int b){
x = a + b;
y = x + b;
}
}
Class static_use {
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
system.out.println(obj1.x + " " + obj2.y);
}
}
a) 7 7
b) 6 6
c) 7 9
d) 9 7
answer: c
15-what is the output of this program?
Class output {
public static void main(String args[])
{
int arr[] = {1, 2, 3, 4, 5};
for ( int i = 0; i < arr.length - 2; ++i)
system.out.println(arr[i] + " ");
}
}
a) 1 2
b) 1 2 3
c) 1 2 3 4
d) 1 2 3 4 5
answer: b
16. in java arrays are
a.objects
b.object references
c.primitive data type
d. none of this
ans: a
17. which one of the following is a valid statement?
a.char[] c = new char();
b.char[] c = new char[5];
c.char[] c = new char(4);
d.char[] c = new char[];
ans: b

18-what is the result of compiling and running the following code?


public Class test{
public static void main(String[] args){
int[] a = new int[0];
system.out.print(a.length);
}
}
a.0
b.compilation error, arrays cannot be initialized to zero size.
c.compilation error, it is a.length() not a.length
d.none of the above
ans: a

19-what will be the output?


public Class test{
public static void main(String[] args){
int[] x = new int[3];
system.out.println("x[0] is " + x[0]);
}
}
a.the program has a compile error because the size of the array wasn't specified when
declaring the array.
b.the program has a runtime error because the array elements are not initialized.
c.the program runs fine and displays x[0] is 0.
d.the program has a runtime error because the array element x[0] is not defined.
ans:c
20-what is the output of the following code?
public Class test{
public static void main(String args[]){
double[] mylist = {1, 5, 5, 5, 5, 1};
double max = mylist[0];
int indexofmax = 0;
for(int i = 1; i < mylist.length; i++){
if(mylist[i] > max){
max = mylist[i];
indexofmax = i;
}
}
system.out.println(indexofmax);
}
}
a.0
b.1
c.2
d.3
e.4
ans:b

21-what is output of the following code:


public Class test{
public static void main(String[] args){
int[] x = {120, 200, 016 };
for(int i = 0; i < x.length; i++)
system.out.print(x[i] + " ");
}
}
a.120 200 16
b.120 200 14
c.120 200 016
d.016 is a compile error. it should be written as 16.
ans: b

22-analyze the following code and choose the correct answer.


int[] arr = new int[5];
arr = new int[6];
a.the code has compile errors because the variable arr cannot be changed once it is assigned.
b.the code has runtime errors because the variable arr cannot be changed once it is assigned.
c.the code can compile and run fine. the second line assigns a new array to arr.
d.the code has compile errors because we cannot assign a different size array to arr.
ans: c

23-what will be the output?


public Class test{
public static void main(String[] args){
int[] a = new int[4];
a[1] = 1;
a = new int[2];
system.out.println("a[1] is " + a[1]);
}
}
a.the program has a compile error because new int[2
b.the program has a runtime error because a[1].
c.a[1] is 0
d.a[1] is 1

ans: c

24-when you pass an array to a method, the method receives ________ .


a.a copy of the array.
b.a copy of the first element.
c.the reference of the array.
d.the length of the array.
ans:c

25-what would be the result of attempting to compile and run the following code?
public Class helloworld{
public static void main(String[] args){
double[] x = new double[]{1, 2, 3};
system.out.println("value is " + x[1]);
}
}
a.the program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by {1, 2, 3}.
b.the program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[3]{1, 2, 3};
c.the program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it
should be replaced by new double[]{1.0, 2.0, 3.0};
d.the program compiles and runs fine and the output is 2.0;
ans: d

26-which will legally declare, construct, and initialize an array?


a.int [] mylist = {};
b.int [] mylist = (5, 8, 2);
c.int mylist [] [] = {4,9,7,0};
d.int mylist [] = {4, 3, 7};
ans: d

27-what will be the output of the program?


public Class test{
public static void main(String [] args)
{
String s1 = args[1];
String s2 = args[2];
String s3 = args[3];
String s4 = args[4];
system.out.print(" args[2] = " + s2);
}
}
and the command-line invocation is c:java> java test 1 2 3 4
a. args[2] = 2
b. args[2] = 3
c. args[2] = null
d.an exception is thrown at runtime.
ans: d

28-what is the value of a[1] after the following code is executed?


int[] a = {0, 2, 4, 1, 3};
for(int i = 0; i < a.length; i++)
a[i] = a[(a[i] + 3) % a.length];
a.0
b.1
c.2
d.3
ans: b

29- which of these operators is used to allocate memory to array variable in java?
a) malloc
b) alloc
c) new
d) new malloc
answer: c
30-which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
answer: d
31- what will be the output of the following java code?

int arr[] = new int [5];


system.out.print(arr);
a) 0
b) value stored in arr[0]
c) 00000
d) class name@ hashcode in hexadecimal form
answer: d
32-which of these is an incorrect statement?
a) it is necessary to use new operator to initialize an array
b) array can be initialized using comma separated expressions surrounded by curly braces
c) array can be initialized when they are declared
d) none of the mentioned
ans: a
33-which of these is necessary to specify at time of array initialization?
a) row
b) column
c) both row and column
d) none of the mentioned
answer: a
34-what will be the output of the following java code?

Class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
system.out.print(array_variable[i] + " ");
i++;
}
}
}
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
ans: a
35-what will be the output of the following java code?
Class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
system.out.println(arr[n] / 2);
}
}
a) 3
b) 0
c) 6
d) 1
ans: d
36-what will be the output of the following java code?
Class array_output
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
system.out.print(array_variable[i] + "");
}
}
}
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
ans: d
37-what will be the output of the following java code?
Class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
system.out.print(sum / 5);
}
}
a) 8
b) 9
c) 10
d) 11
ans: b
38-how many of the following are legal declarations?
[]double lion;
double[] tiger;
double bear[];
a. none
b. one
c. two
d. three
ans: c
39-how do you determine the number of elements in an array?
int buses[] = new int[5];
a. buses.length
b. buses.length()
c. buses.size
d. buses.size()
ans: a
40-which of the following create an empty two-dimensional array with dimensions 2×2?
a. int[][] blue = new int[2, 2];
b. int[][] blue = new int[2], [2];
c. int[][] blue = new int[2][2];
d. int[][] blue = new int[2 x 2];
ans: c
41-how many of the following are legal declarations?
String lion [] = new String[] {"lion"};
String tiger [] = new String[1] {"tiger"};
String bear [] = new String[] {};
String cat [] = new String[0] {};
a. none
b. one
c. two
d. three
ans: c
42-how many of the following are legal declarations?
float[] lion = new float[];
float[] tiger = new float[1];
float[] bear = new[] float;
float[] cat = new[1] float;
a. none
b. one
c. two
d. three
ans: b
43-which is not a true statement about an array?
a. an array expands automatically when it is full.
b. an array is allowed to contain duplicate values.
c. an array understands the concept of ordered elements.
d. an array uses a zero index to reference the first element.
ans: a
44-what is a possible output of the following code?
String[] strings = new String[2];
system.out.println(strings);
a. [null, null]
b. [,]
c. [ljava.lang.string;@74a14482
d. none of the above
ans: c
45-what does the following output?
String[] os = new String[] { "mac", "linux", "windows" };
arrays.sort(os);
system.out.println(arrays.binarysearch(os, "mac"));
a. 0
b. 1
c. 2
d. the output is not defined.
ans: b
46-java array is a collection of ________.
a. similar type of elements
b. different type of element
c. heterogeneous data
d. both a and c
ans: a
47-array data access using _____.
a. operator
b. variable
c. index
d. pointer
ans: c
48-java array can allocate __________.
a. dynamic memory
b. static memory
c. both a and b
d. none of the above
ans: b
49-which of the following is an incorrect array declaration?
a. int [] arr = new int[5].
b. int arr[] = new int[5].
c. int arr[] = new int[5].
d. int arr[] = int [5] new
ans: d
50-index in array start with ______.
a. -1
b. 0
c. 1
d. infinite
ans: b
51- which of the following is used to declare,construct, and initlaize an array?
a. int arr [] [] = {1, 2, 3, 4};
b. int [] arr = (1, 2, 3);
c. int [] arr = {};
d. int arr [] = {1, 2, 3};
ans: d
52-which of the following is advantage of java array?
a. code optimization
b. random access
c. size no-limit
d. both a and b
ans: a
53-in java, array elements are stored in ________ memory locations.
a. random
b. sequential
c. sequential & random
d. binary search
ans: b
54-the java virtual machine (jvm) implements arrays as ___ type.
a) primitive
b) object
c) -
d) –
ans: b
55-unlike c-arrays, the java-arrays have ___.
a) names
b) values
c) methods and fields
d) none
ans: c
56-an array declaration in java without initialization ___ memory.
a) does not allocate
b) allocates memory
c) -
d) –
ans: a
57-which are the special symbols used to declare an array in java?
a) braces { }
b) parentheses ()
c) square brackets [ ]
d) angled brackets < >
ans: c
58-which are the special symbols used to initialize an array at the time of the declaration
itself?
a) parentheses ( )
b) square brackets [ ]
c) braces { }
d) angled brackets < >
ans: c
59-it is possible to skip initializing some elements of the array during shorthand initialization.
(true / false)
a) false
b) true
c) -
d) –
ans: a
60-in java, an array can be declared without initialization without mentioning the size. (true /
false)
a) true
b) false
c) -
d) –
ans: a
61-what is the output of the below java code snippet with arrays?
static int[] nums;
public static void main(String args[])
{
system.out.println(nums.length);
}
a) 0
b) null
c) compiler error
d) runtime exception - null pointer exception
ans: d
62-what is the output of the below java program?

int[] marks = {35,65,95};


system.out.print(marks.length + "," + marks[1]);
a) 2,65
b) 3,95
c) 3,65
d) compiler error
ans: c
63-what is the output of the below java code snippet?
int[] balls = {};
system.out.print(balls.length);
a) 0
b) -1
c) 1
d) compiler error
ans: a
64-which is the correct way of knowing array size in java?
a) ary.length()
b) ary.length
c) ary->length()
d) ary->length
ans: b
65-what is the output of the below java program with arrays?
String[] colors = {"red";"yellow";"white"};
system.out.print(colors[2]);
a) red
b) yellow
c) white
d) compiler error
ans: d
66-what is the output of the below java program with arrays?
public Class polo {
public static void main(String args[])
{
String[] computer = {"ram","hdd","mouse"};
String[] parts = {computer[0],computer[2]};
system.out.print(parts[1]);
}
}
a) ram
b) hdd
c) mouse
d) compiler error
ans: c
67-what is the output of the below java program?
int ages[3] = {25, 27, 30};
system.out.println(ages[1]);
a) 25
b) 27
c) 30
d) compile error
ans: d
68-we should not specify the array size if declaration and initialization are done at the same
time. (true / false)
a) false
b) true
c) -
d) –
ans: b
69-if an index of an element is n, what is its actual position in the array?
a) n-1
b) n
c) n+1
d) n+2
ans: c
70-an array in java can be declared only of some predefined types. (true/false)
a) false
b) true
c) -
d) –
ans: a
71-the name of an array variable or identifier can start with ___.
a) a letter
b) underscore ( _ )
c) dollar symbol ($)
d) all
ans: d
72-shorthand array initialization in java needs the keyword "new" to allocate memory to the
array and elements. state true or false.
a) false
b) true
c) -
d) –
ans: a
73-lazy initialization of array requires the keyword "new" to allocate memory to the array and
its elements. state true or false.
a) false
b) true
c) -
d) –
ans: b
74-what is the default value of an element of object type array?
a) 0
b) null
c) -1
d) garbage value
ans: b
75-what is the default value of byte, short, int or long data type elements of an array in java?
a) -1
b) 1
c) 0
d) garbage value
ans: c
75-what is the default value of float or double data type elements of an array in java?
a) 0
b) 0.0
c) 1
d) 1.0
ans: b
76-what is the default value of a char data type elements of an array in java?
a) 'a'
b) '\0'
c) null
d) '\0' or null
ans: d
77-allocating memory with the keyword "new" causes the array elements to carry default
values. state true or false.
a) false
b) true
c) -
d) –
ans: b
78-what is the output of the below java program?
int balls[], rounds=3;
balls = new int[rounds];
for(int i=0; i<balls.length; i++)
balls[i] = (i+1)*2;
for(int j=0; j<balls.length; j++)
system.out.print(balls[j] + ",");
a) 0,2,4,
b) 1,2,3,
c) 2,4,6,
d) compiler error
ans: c
79-what is the output of the below java program with arrays?
String[] ary = {"kite", "air"};
String str = "plane";
ary[1] = str;
str = "fly";
system.out.println(ary[1]);
a) air
b) plane
c) fly
d) compiler error
ans: b
80-an array of arrays in java is called ___ array.
a) bidirectional
b) combo
c) multidimensional
d) multi-value
ans: c
81-a multidimensional array contains elements of the same data-type in all rows and columns.
state true or false.
a) false
b) true
c) -
d) –
ans: b
82-row number and column number in a multidimensional array start with ___.
a) -1
b) 0
c) 1
d) 2
ans: b
83- a 4-dimensional array is an array of ___ dimensional arrays.
a) 4
b) 3
c) 2
d) 1
ans: b
84-choose the correct way of initializing a multidimensional array below.
a) int[][] code = {{1,2},{3,4,5}};
b) int[2][] code = {{1,2},{3,4,5}};
c) int[][] code={1,2,3,4,5};
d) all
ans: a
85-what is the output of the java program with the multidimensional array?
int[][] goats;
goats = new int[3][];
goats[0] = {1,2};
system.out.println(goats[0][1]);
a) 0
b) 1
c) 2
d) compiler error
ans: d
86-state true or false. in a multidimensional array, the number of columns in each row can be
different.
ex.
1
23
456
a) false
b) true
c) -
d) –
ans:b
87-while mentioning the array size in a multidimensional array using the new keyword, the
left-most script is mandatory. state true or false.
int ary[][];
ary = new int[5][];//first dimension is compulsory.
a) false
b) true
c) -
d) –
ans: b
88-which of these is an incorrect statement?
a. it is necessary to use new operator to initialize an array.
b. array can be initialized using comma separated expressions surrounded by curly braces.
c. array can be initialized when they are declared.
d. none of the mentioned
ans: a
89-which of following is static array?
int arr[] = { 1, 3, 4 };
int* arr2 = new int[3];
A) arr
B) arr2
C) arr and arr2 both
D) none of this
ans: a
90) array.sort method contained in package __________.
a) java.lang
b)java.util
c)java.math
d)none of this
ans: b

You might also like