Java As Object Oriented Programming Language: Unit IV
Java As Object Oriented Programming Language: Unit IV
Java As Object Oriented Programming Language: Unit IV
Unit IV | 4.1
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Explanation : Type casting a larger variable into a smaller variable results in modulo
of larger variable by range of smaller variable. b contains 300 which is larger than
byte’s range i : e -128 to 127 hence d contains 300 modulo 256 i : e 44.
Output :
$ javac conversion.java
$ java conversion
39 44
9. What is the output of this program?
class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
(a) 25 (b) 24 (c) 32 (d) 33
Answer : c
Explanation : Operator ++ has more preference than *, thus g becomes 4 and when
multiplied by 8 gives 32.
Output :
$ javac increment.java
$ java increment
32
10. What is the output of this program?
class area {
public static void main(String args[])
{
double r, pi, a;
r = 9.8;
pi = 3.14;
a = pi * r * r;
System.out.println(a);
}
}
(a) 301.5656 (b) 301 (c) 301.56 (d) 301.56560000
Answer : a
Unit IV | 4.3
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Explanation : None.
Output :
$ javac area.java
$ java area
301.5656
11. What is the numerical range of a char in Java?
(a) -128 to 127 (b) 0 to 256
(c) 0 to 32767 (d) 0 to 65535
Answer : d
Explanation : Char occupies 16-bit in memory, so it supports 2^16 i:e from 0 to
65535.
12. Which of these coding types is used for data type characters in Java?
(a) ASCII (b) ISO-LATIN-1
(c) UNICODE (d) None of the mentioned
Answer : c
Explanation : Unicode defines fully international character set that can represent all
the characters found in all human languages. Its range is from 0 to 65536.
13. Which of these values can a boolean variable contain?
(a) True and False(b) 0 and 1 (c) Any integer value (d) true
Answer : a
Explanation : Boolean variable can contain only one of two possible values, true and
false.
14. Which of these occupy first 0 to 127 in Unicode character set used for characters in
Java?
(a) ASCII (b) ISO-LATIN-1
(c) None of the mentioned (d) ASCII and ISO-LATIN1
Answer : d
Explanation : First 0 to 127 character set in Unicode are same as those of ISO-LAIN-1
and ASCII.
15. Which one is a valid declaration of a boolean?
(a) boolean b1 = 1; (b) boolean b2 = ‘false’;
(c) boolean b3 = false; (d) boolean b4 = ‘true’
Answer : c
Explanation : Boolean can only be assigned true or false literals.
Unit IV | 4.4
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
25. Which of these can not be used for a variable name in Java?
(a) identifier (b) keyword
(c) identifier and keyword (d) None of the mentioned
Answer : b
Explanation : Keywords are specially reserved words which can not be used for
naming a user defined variable, example : class, int, for etc.
26. What is the output of this program?
class evaluate {
public static void main(String args[])
{
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
System.out.println(sum);
}
}
(a) 38 (b) 39 (c) 40 (d) 41
Answer : c
Explanation : None
Output :
$ javac evaluate.java
$ java evaluate
40
27. What is the output of this program?
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/2;
array_variable[i]++;
System.out.print(array_variable[i] + " ");
i++;
}
}
}
Unit IV | 4.8
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Explanation : arr is an array variable, it is pointing to array if integers. Printing arr will
print garbage value. It is not same as printing arr[0].
34. 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.
Answer : a
Explanation : Array can be initialized using both new and comma separated
expressions surrounded by curly braces example : int arr[5] = new int[5]; and int arr[]
= { 0, 1, 2, 3, 4};
35. 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
Explanation : None.
36. What is the output of this program?
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) 02468 (b) 13579
(c) 0123456789 (d) 1 2 3 4 5 6 7 8 9 10
Answer : a
Explanation : When an array is declared using new operator then all of its elements
are initialized to 0 automatically. for loop body is executed 5 times as whenever
controls comes in the loop i value is incremented twice, first by i++ in body of loop
then by ++i in increment condition of for loop.
Unit IV | 4.11
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Output :
$ javac array_output.java
$ java array_output
02468
37. What is the output of this program?
class multidimention_array {
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System.out.print(sum);
}
}
(a) 11 (b) 10 (c) 13 (d) 14
Answer : b
Explanation : arr[][] is a 2D array, array has been allotted memory in parts. 1st row
contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements.
each element of array is given i + j value in loop. sum contains addition of all the
elements of the array.
Output :
$ javac multidimention_array.java
$ java multidimention_array
10
Unit IV | 4.12
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
44. Which of these jump statements can skip processing remainder of code in its body
for a particular iteration?
(a) break (b) return (c) exit (d) continue
View Answer
Answer : d
Explanation : None.
45. Which of these statement is correct?
(a) Switch statement is more efficient than a set of nested ifs.
(b) Two case constants in the same switch can have identical values.
(c) Switch statement can only test for equality, whereas if statement can evaluate
any type of boolean expression.
(d) It is possible to create a nested switch statements.
Answer : b
Explanation : No two case constants in the same switch can have identical values.
46. What is the output of this program?
class selection_statements {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
if ((var2 = 1) == var1)
System.out.print(var2);
else
System.out.print(++var2);
}
}
(a) 1 (b) 2 (c) 3 (d) 4
Answer : b
Explanation : var2 is initialised to 1. The conditional statement returns false and the
else part gets executed.
Output :
$ javac selection_statements.java
$ java selection_statements
2
Unit IV | 4.15
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Unit IV | 4.19
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
(a) 9 (b) 8
(c) Compilation error (d) Runtime error
Answer : c
Explanation : Two variables with the same name can’t be created in a class.
Output :
$ javac main_class.java
Exception in thread “main” java.lang.Error : Unresolved compilation problem :
Duplicate local variable x
57. Which of the following statements is correct?
(a) Public method is accessible to all other classes in the hierarchy
(b) Public method is accessible only to subclasses of its parent class
(c) Public method can only be called by object of its class.
(d) Public method can be accessed by calling object of the public class.
Answer : a
Explanation : None.
58. What is the output of this program?
class box {
int width;
int height;
int length;
}
class mainclass {
public static void main(String args[])
{
box obj = new box();
obj.width = 10;
obj.height = 2;
obj.length = 10;
int y = obj.width * obj.height * obj.length;
System.out.print(y);
}
}
(a) 12 (b) 200 (c) 400 (d) 100
Answer : b
Explanation : None.
Unit IV | 4.20
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Output :
$ javac mainclass.java
$ java mainclass
200
59. What is the output of this program?
class box {
int width;
int height;
int length;
}
class mainclass {
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}
(a) 1 (b) 2
(c) Runtime error (d) Garbage value
Answer : a
Explanation : When we assign an object to another object of same type, all the
elements of right side object gets copied to object on left side of equal to, =,
operator.
Output :
$ javac mainclass.java
$ java mainclass
1
Unit IV | 4.21
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Unit IV | 4.22
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
64. Which of these method of class String is used to extract a single character from a
String object?
(a) CHARAT() (b) chatat()
(c) charAt() (d) ChatAt()
Answer : c
Explanation : None.
65. Which of these constructors is used to create an empty String object?
(a) String() (b) String(void)
(c) String(0) (d) None of the mentioned
Answer : a
Explanation : None.
66. Which of these is an incorrect statement?
(a) String objects are immutable, they cannot be changed.
(b) String object can point to some other reference of String variable.
(c) StringBuffer class is used to store string in a buffer for later use.
(d) None of the mentioned.
Answer : c
Explanation : StringBuffer class is used to create strings that can be modified after
they are created.
67. What is the output of this program?
class String_demo {
public static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
System.out.println(s);
}
}
(a) a (b) b (c) c (d) abc
Answer : d
Explanation : String(chars) is a constructor of class string, it initializes string s with
the values stored in character array chars, therefore s contains “abc”.
Output :
$ javac String_demo.java
$ java String_demo
abc
Unit IV | 4.23
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Unit IV | 4.25
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
84. Which operator is used by Java run time implementations to free the memory of an
object when it is no longer needed?
(a) delete (b) free
(c) new (d) None of the mentioned
View Answer
Answer : d
Explanation : Java handles deallocation of memory automatically, we do not need to
explicitly delete an element. Garbage collection only occurs during execution of the
program. When no references to the object exist, that object is assumed to be no
longer needed, and the memory occupied by the object can be reclaimed.
85. Which function is used to perform some action when the object is to be destroyed?
(a) finalize() (b) delete()
(c) main() (d) None of the mentioned
View Answer
Answer : a
Explanation : None.
86. What is the output of this program?
class box {
int width;
int height;
int length;
int volume;
box() {
width = 5;
height = 5;
length = 6;
}
void volume() {
volume = width*height*length;
}
}
class constructor_output {
public static void main(String args[])
{
box obj = new box();
obj.volume();
System.out.println(obj.volume);
}
}
Unit IV | 4.28
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
int volume;
void finalize() {
volume = width*height*length;
System.out.println(volume);
}
protected void volume() {
volume = width*height*length;
System.out.println(volume);
}
}
class Output {
public static void main(String args[])
{
box obj = new box();
obj.volume();
}
}
(a) 150 (b) 200
(c) Runtime error (d) Compilation error
Answer : a
Explanation : None.
Output :
$ javac Output.java
$ java Output
150
89. Which of the following statements are incorrect?
(a) Default constructor is called at the time of declaration of the object if a
constructor has not been defined.
(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
Explanation : finalize() method is called just prior to garbage collection. it is not
called when object goes out of scope.
Unit IV | 4.30
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) JAVA AS OBJECT ORIENTED …
Unit IV | 4.32