NEW
Unit III
STRUCTURING OF PROGRAM
MULTIPLE CHOICE QUESTIONS
1. Which of the following is the functionality of ‘Data Abstraction’?
(a) Reduce Complexity (b) Binds together code and data
(c) Parallelism (d) None of the mentioned
Answer : a
Explanation : An essential element of Object Oriented Programming is ‘Data
Abstraction’ which means hiding things. Complexity is managed through abstraction.
2. Which of the following mechanisms is/are provided by Object Oriented Language to
implement Object Oriented Model?
(a) Encapsulation (b) Inheritance
(c) Polymorphism (d) All of the mentioned
Answer : d
Explanation : None.
3. Which of these is the functionality of ‘Encapsulation’?
(a)
Binds together code and data
(b) Using single interface for general class of actions.
(c)
Reduce Complexity
(d) All of the mentioned
Answer : a
Explanation : ‘Encapsulation’ acts as protective wrapper that prevents code and data
from being accessed by other code defined outside the wrapper.
4. What is ‘Basis of Encapsulation’?
(a) object (b) class
(c) method (d) all of the mentioned
Answer : d
Explanation : Encapsulation is the mechanism that binds together code and data it
manipulates, and keeps both safe from outside interface and misuse. Class, which
contains data members and methods is used to implement Encapsulation.
Unit III | 3.1PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
5. How will a class protect the code inside it?
(a) Using Access specifiers (b) Abstraction
(c) Use of Inheritance (d) All of the mentioned
Answer : a
Explanation : Each method or variable in a class may be marked ‘public’ or ‘private’.
They are called Access Specifiers.
6. What is the output of this program?
class Test {
int a;
public int b;
private int c;
}
class AcessTest {
public static void main(String args[])
{
Test ob = new Test();
ob.a = 10;
ob.b = 20;
ob.c = 30;
System.out.println(" Output :a, b, and c" + ob.a + " " + ob.b + " " + ob.c”);
}
}
(a) Compilation error (b) Run time error
(c) (d) None of the mentioned
Output : a, b and c 10 20 30
Answer : a
Explanation : Private members of a class cannot be accessed directly. In the above
program, the variable c is a private member of class ‘Test’ and can only be accessed
through its methods.
7. Which of the following is a mechanism by which object acquires the properties of
another object?
(a) Encapsulation (b) Abstraction
(c) Inheritance (d) Polymorphism
Answer : c
Explanation : ‘Inheritance’ is the mechanism provided by Object Oriented Language,
which helps an object to acquire the properties of another object usually child object
from parent object.
Unit III | 3.2PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
8. Which of the following supports the concept of hierarchical classification?
(a) Polymorphism (b) Encapsulation
(c) Abstraction (d) Inheritance
Answer : d
Explanation : Use of Hierarchical classification avoids defining the properities of
object explicitly at each level which have acquired their properties from higher levels.
9. Which Keyword from the following is used to inherit properties from one class into
another?
(a) extends (b) subclasses
(c) native (d) all of the mentioned
Answer : a
Explanation : None.
10. Which of the following concept is often expressed by the phrase, ‘One interface,
multiple methods’?
(a) Abstraction (b) Polymorphism
(c) Inheritance (d) Encapsulation
Answer : b
Explanation : None.
11. Who is known as father of Java Programming Language?
(a) James Gosling (b) M. P Java
(c) Charel Babbage (d) Blais Pascal
Answer : a
12. In java control statements break, continue, return, try-catch-finally and assert belongs
to?
(a) Selection statements (b) Loop Statements
(c) Transfer statements (d)
Pause Statement
Answer : c
13. Which provides runtime environment for java byte code to be executed?
(a)
JDK
(b)
JVM
(c) JRE
Answer : b
14. What is byte code in Java?
(a) Code generated by a Java compiler
(b) Code generated by a Java Virtual Machine
(c) Name of Java source code file
(d) Block of code written inside a class
Answer : a
Unit III | 3.3
(d)
JAVACPRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
15. Which of the following are not Java keywords ?
(a)
double
(b)
switch
(c) then
(d) instance of
Answer : c
16. Which of these have highest precedence?
(a)
()
(b)
++
(c) *
(d) >
Answer : a
17. Which of these is returned by operator '&' ?
(a)
Integer
(b) Character
(c) Boolean
(d) Float
Answer : b
18. Data type long literals are appended by _____
(a) Uppercase L (b)
Lowercase L
(c) Long (d) Both A and B
Answer : d
19. Which variables are created when an object is created with the use of the keyword
'new' and destroyed when the object is destroyed?
(a) Local variables (b)
Instance variables
(c) Class Variables (d) Static variables
Answer : b
20. Java language was initially called as _____
(a)
Sumatra
(b)
J++
(c) Oak
(d) Pine
Answer : c
21. What is garbage collection in the context of Java?
(a) Java deletes all unused java files on the system.
(b) Memory used by the object with no reference is automatically reclaimed.
(c) The JVM cleans output of Java program with error.
(d) Any unused package in a program automatically gets deleted.
Answer : b
22. Which one is a template for creating different objects ?
(a) An Array (b)
A class
(c) Interface (d) Method
Answer : b
23. Which symbol is used to contain the values of automatically initialized arrays?
(a)
Brackets
(b)
Braces
(c) Parentheses
Answer : b
Unit III | 3.4
(d) CommaPRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
24. Which one is true about a constructor ?
(a) A constructor must have the same name as the class it is declared within.
(b) A constructor is used to create objects.
(c) A constructor may be declared private
(d) All of the above
Answer : d
25. Which of these operators is used to allocate memory to array variable in Java?
(a)
alloc
(b)
malloc
(c) new malloc
(d) new
Answer : d
26. Which of these is not a bitwise operator?
(a) &' Operator (b) &=' Operator
(c) |=' Operator (d) <=' Operator
Answer : d
27. Which of these is returned by Greater Than, Less Than and Equal To (i.e Relational)
operator ?
(a)
Float
(b)
Integer
(c) Boolean
(d) Double
Answer : c
28. Which statement transfer execution to different parts of your code based on the
value of an expression?
(a)
If
(b)
Switch
(c) Nested-if
(d) if-else-if
Answer : b
29. Modulus operator (%) can be applied to which of these?
(a) Integers (b)
Floating - point numbers
(c) Both A and B (d) None of These
Answer : c
30. What feature of OOP has a super-class sub-class concept?
(a) Hierarchical inheritance
(b) Single inheritance
(c) Multiple inheritances
(d) Multilevel inheritance
Answer : a
31. Which of the following are not the methods of the Thread class?
(a) yield() (b)
(c) go() (d) stop()
Answer : c
Unit III | 3.5
sleep(long msec)PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
32. Division operator has _____ precedence over multiplication operator.
(a)
Highest
(b)
Least
(c) Equal
(d) None of These
Answer : c
33. What is the full form of JVM ?
(a) Java Very Large Machine (b) Java Verified Machine
(c) Java Very Small Machine (d) Java Virtual Machine
Answer : d
34. In Java code, the line that begins with /* and ends with */ is known as?
(a) Multiline comment (b)
Single line comment
(c) Both (a) and (b) (d) None of these
Answer : a
35. Which of the following are not Java modifiers?
(a)
public
(b)
private
(c) friendly
(d) transient
Answer : c
36. Which of the following factors supports the statement that the 'reusability' is a
desirable feature of a language?
(a) It decreases the testing time. (b) It lowers the maintenance cost.
(c) It reduces the compilation time. (d) Both 1 and 2.
Answer : d
37. Which of these can be used to fully abstract a class from its implementation?
(a) Objects (b) Packages
(c) Interfaces (d) None of the Mentioned.
Answer : c
Explanation : None.
38. Which of these access specifiers can be used for an interface?
(a) Public (b) Protected
(c) private (d) All of the mentioned
Answer : d
39. Which of these keywords is used by a class to use an interface defined previously?
(a)
import
(b) Import
(c) implements
(d) Implements
Answer : c
Explanation : interface is inherited by a class using implements.
Unit III | 3.6PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
40. Which of the following is correct way of implementing an interface salary by class
manager?
(a)
class manager extends salary {}
(b) class manager implements salary {}
(c)
class manager imports salary {}
(d) None of the mentioned.
Answer : b
Explanation : None.
41. Which of the following is incorrect statement about packages?
(a)
Interfaces specifies what class must do but not how it does.
(b) Interfaces are specified public if they are to be accessed by any code in the
program.
(c)
All variables in interface are implicitly final and static.
(d) All variables are static and methods are public if interface is defined pubic.
Answer : d
Explanation : All methods and variables are implicitly public if interface is declared
public.
42. Which of the following package stores all the standard java classes?
(a)
lang
(b) java
(c) util
Answer : b
Explanation : None.
43. What is the output of this program?
interface calculate {
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
x = item * item;
}
}
class interfaces {
public static void main(String args[]) {
display arr = new display;
arr.x = 0;
Unit III | 3.7
(d) java packagesPRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
arr.cal(2);
System.out.print(arr.x);
}
}
(a) 0 (b) 2
(c) 4 (d) None of the mentioned
Answer : c
Explanation : None.
Output :
$ javac interfaces.java
$ java interfaces
4
44. What is the output of this program?
interface calculate {
void cal(int item);
}
class displayA implements calculate {
int x;
public void cal(int item) {
x = item * item;
}
}
class displayB implements calculate {
int x;
public void cal(int item) {
x = item / item;
}
}
class interfaces {
public static void main(String args[]) {
displayA arr1 = new displayA;
displayB arr2 = new displayB;
arr1.x = 0;
arr2.x = 0;
Unit III | 3.8PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
arr1.cal(2);
arr2.cal(2);
System.out.print(arr1.x + " " + arr2.x);
}
}
(a)
0 0
(b) 2 2
(c) 4 1
(d) 1 4
Answer : c
Explanation : class displayA implements the interface calculate by doubling the value
of item, where as class displayB implements the interface by dividing item by item,
therefore variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output :
$ javac interfaces.java
$ java interfaces
4 1
45. What is the output of this program?
interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item<2)
x = VAR;
else
x = item * item;
}
}
class interfaces {
public static void main(String args[]) {
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
Unit III | 3.9PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x); } }
(a)
0 1 2
(b) 0 2 4
(c) 0 0 4
(d) 0 1 4
Answer : c
Explanation : None.
Output :
$ javac interfaces.java
$ java interfaces
0 0 4
46. What will be the output of following program?
#include<iostream.h>
void main()
{
float x;
x=(float)9/2;
cout<<x;
}
(a)
4.5
(b) 4.0
(c) 4
(d) 5
Answer : a
47. The term _____ means the ability to take many forms.
(a)
Inheritance
(b) Polymorphism
(c) Member function (d) Encapsulation
Answer : b
48. Runtime polymorphism is achieved by
(a) Friend function (b) Virtual function
(c) Operator overloading (d) Function overloading
Answer : b
49. Access to private data
(a)
Restricted to methods of the same class
(b) Restricted to methods of other classes
(c)
Available to methods of the same class and other classes
(d) Not an issue because the program will not compile
Answer : b
Unit III | 3.10PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
50. Additional information sent when an exception is thrown may be placed in
(a) The throw keyword (b) The function that caused the error
(c) The catch block (d) An object of the exception class
Answer : c
51. A static data member is given a value
(a) Within the class definition (b) Outside the class definition
(c) When the program is executed (d) Never
Answer : d
52. What will be the result of the expression 13 and 25?
(a) 38
(b) 25
(c) 9
(d) 12
Answer : c
53. In a class specifier ,data or function designated private are accessible
(a)
To any function in the program
(b) Only if you the password
(c)
To member functions of that class
(d) Only to public members of the class
Answer : c
54. Which of the statements are true ?
I. Function overloading is done at compile time.
II. Protected members are accessible to the member of derived class.
III. A derived class inherits constructors and destructors.
IV. A friend function can be called like a normal function.
V. Nested class is a derived class.
(a) I, II, III
(b) II, III, V
(c) III, IV, V
(d) I, II, IV
Answer : d
55. At which point of time a variable comes into existence in memory is determined by
its
(a)
Scope
(b) Storage class
(c) Data type
(d) All of the above
Answer : b
56. When the compiler cannot differentiate between two overloaded constructors, they
are called
(a)
Overloaded (b) Destructed
(c) Ambiguous
Answer : c
Unit III | 3.11
(d) DubiousPRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
57. The actual source code for implementing a template function is created when
(a)
The declaration of function appears.
(b) The function is invoked.
(c)
The definition of the function appears.
(d) None of the above.
Answer : b
58. Usually a pure virtual function
(a)
Has complete function body
(b) Will never be called
(c)
Will be called only to delete an object
(d) Is defined only in derived class
Answer : d
59. Which of the following is the valid class declaration header for the derived class d
with base classes b1 and b2?
(a) class d : public b1, public b2 (b) class d : class b1, class b2
(c) class d : public b1, b2 (d) class d : b1, b2
Answer : a
60. The process of extracting the relevant attributes of an object is known as
(a) Polymorphism (b) Inheritence
(c) Abstraction (d) Data hiding
Answer : b
61. What features make C++ so powerful ?
(a) Easy implementation (b) Reusing old code
(c) Reusing old code (d) All of the above
Answer : d
62. Which of the following operator can be overloaded through friend function?
(a)
->
(b) =
(c) ( )
Answer : d
63. The keyword friend does not appear in
(a)
The class allowing access to another class
(b) The class desiring access to another class
(c)
The private section of a class
(d) The public section of a class
Answer : c
Unit III | 3.12
(d) *PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
64. Exception handling is targeted at
(a) Run-time error (b) Compile time error
(c) Logical error (d) All of the above
Answer : a
65. Function templates can accept
(a)
Any type of parameters
(b) Only one parameter
(c)
Only parameters of the basic type
(d) Only parameters of the derived type
Answer : c
66. If the variable count exceeds 100, a single statement that prints “Too many” is
(a)
if (count<100) cout << “Too many”;
(b) if (count>100) cout >> “Too many”;
(c)
if (count>100) cout << “Too many”;
(d) None of these.
Answer : c
67. The mechanism that binds code and data together and keeps them secure from
outside world is known as
(a)
Abstraction
(b) Inheritance
(c) Encapsulation
(d) Polymorphism
Answer : c
68. The operator << when overloaded in a class
(a) must be a member function (b) must be a non member function
(c) can be both (a) and (b) above (d) cannot be overloaded
Answer : c
69. To access the public function fbase() in the base class, a statement in a derived class
function fder() uses the statement.fbase();
(a) fbase(); (b) fder();
(c) base::fbase(); (d) der::fder();
Answer : a
70. In which case is it mandatory to provide a destructor in a class?
(a)
Almost in every class
(b) Class for which two or more than two objects will be created
(c)
Class for which copy constructor is defined
(d) Class whose objects will be created dynamically
Answer : d
Unit III | 3.13PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
71. _____ members of a base class are never accessible to a derived class.
(a)
Public
(b) Private
(c) Protected
(d) (a), (b) and (c)
Answer : b
72. What is the error in the following code?
class t
{
virtual void print();
}
(a)
No error
(b) Function print() should be declared as static.
(c)
Function print() should be defined.
(d) Class t should contain data members.
Answer : a
73. It is possible to declare as a friend
(a) A member function (b) A global function
(c) A class (d)
All of the above
Answer : d
74. A struct is the same as a class except that
(a)
There are no member functions
(b) All members are public
(c)
Cannot be used in inheritance hierarchy
(d) It does have a this pointer
Answer : c
75. C++ was originally developed by
(a) Clocksin and Melish (b) Donald E.Knuth
(c) Sir Richard Hadlee (d) Bjarne Stroustrup
Answer : d
76. What is the output of the following code
char symbol[3]={‘a’,‘b’,‘c’};
for (int index=0; index<3; index++)
cout << symbol [index];
(a)
a b c
(b) “abc”
(c) abc
Answer : c
Unit III | 3.14
(d) ‘abc’PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
77. If we create a file by ‘ifstream’, then the default mode of the file is _____
(a)
ios :: out
(b) ios :: in
(c) ios :: app
(d) ios :: binary
Answer : b
78. The following can be declared as friend in a class
(a)
An object
(b) A class
(c)
A public data member
(d) A private data member
Answer : b
79. The polymorphism can be characterized by the phrase
(a)
One interface,multiple methods
(b) Multiple interfaces,one method
(c)
One interface,one method
(d) None of the above
Answer : a
80. A virtual class is the same as
(a) An abstract class (b) A class with a virtual function
(c) A base class (d) None of the above
Answer : d
81. Member functions, when defined within the class specification
(a)
Are always inline
(b) Are not inline
(c)
Are inline by default, unless they are too big or too complicated
(d) Are not inline by default.
Answer : a
82. Assume that we have constructor functions for both base class and derived class.
Now consider the declaration in main( ). Base * P = New Derived; in what sequence
will the constructor be called ?
(a)
Derived class constructor followed by Base class constructor.
(b) Base class constructor followed by derived class constructor.
(c)
Base class constructor will not be called.
(d) Base class constructor will not be called.
Answer : b
83. The operator that cannot be overloaded is
(a)
++
(b) : :
(c) ~
Answer : b
Unit III | 3.15
(d) ( )PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
84. Which of the following declarations are illegal?
(a) void *ptr; (b) char *str = “hello”;
(c) char str = “hello”; (d) const *int p1;
Answer : c
85. Identify the operator that is NOT used with pointers
(a)
->
(b) &
(c) *
(d) >>
Answer : d
86. Which of the following statements is NOT valid about operator overloading?
(a)
Only existing operators can be overloaded
(b) Overloaded operator must have at least one operand of its class type
(c)
The overloaded operators follow the syntax rules of the original operator
(d) None of the above
Answer : d
87. Overloading a postfix increment operator by means of a member function takes
(a) No argument (b) One argument
(c) Two arguments (d) Three arguments
Answer : a
88. Which of the following will produce a value 10 if x = 9.7?
(a)
floor(x)
(b) abs(x)
(c) log(x)
(d) ceil(x)
Answer : d
89. Which of the following is not the characteristic of constructor?
(a)
They should be declared in the public section.
(b) They do not have return type.
(c)
They can not be inherited.
(d) They can be virtual.
Answer : d
90. You may override the class access specifiers
(a)
Public members
(b) Public and protected members
(c)
Any specific class members you choose
(d) No class members
Answer : c
91. You separated a derived class name from its access specifier with
(a)
A colon
(b) Two colons
(c) Atleast one space (d) A semi colon
Answer : b
Unit III | 3.16PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
92. Consider the following statements :
int x = 22,y=15;
x = (x>y) ? (x+y) : (x-y);
What will be the value of x after executing these statements?
(a)
22
(b) 37
(c) 7
(d) 5
Answer : b
93. A friend function to a class, C cannot access
(a)
Private data members and member functions
(b) Public data members and member functions
(c)
Protected data members and member functions
(d) The data members of the derived class of C
Answer : d
94. The members of a class by default are
(a) Public (b) Protected
(c) Private (d) Mandatory to specify
Answer : c
95. Which operator is used to signify the namespace?
(a) conditional operator (b) ternary operator
(c) scope operator (d) none of the mentioned
Answer : c
Explanation : scope operator
96. Identify the correct statement
(a)
Namespace is used to group class, objects and functions.
(b) Namespace is used to mark the beginning of the program.
(c)
Namespace is used to seperate the class, objects.
(d) None of the above
Answer : a
Explanation : Namespace allow you to group class, objects and functions. It is used
to divide the global scope into the sub-scopes.
97. What is the use of Namespace?
(a) To encapsulate the data (b) To structure a program into logical units.
(c) Both a and b (d) none of the mentioned
Answer : b
Explanation : The main aim of the namespace is to understand the logical units of
the program and to make the program so robust.
Unit III | 3.17PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
STRUCTURING OF PROGRAM
98. What is the general syntax for accessing the namespace variable?
(a) namespaceid::operator (b) namespace,operator
(c) namespace#operator (d) none of the mentioned
Answer : a
Explanation : namespaceid::operator
99. Which keyword is used to access the variable in namespace?
(a)
using
(b) dynamic
(c) const
(d) static
Answer : a
100. Which of these keywords is used to define interfaces in Java?
(a)
interface
(b) Interface
(c) intf
Answer : a
Explanation : None.
Unit III | 3.18
(d) Intf
Unit IV
JAVA AS OBJECT ORIENTED
PROGRAMMING LANGUAGE
MULTIPLE CHOICE QUESTIONS
1. What is the range of data type short in Java?
(a) -128 to 127
(b) -32768 to 32767
(c) -2147483648 to 2147483647
(d) None of the mentioned
Answer : b
Explanation : Short occupies 16 bits in memory. Its range is from -32768 to 32767.
2. What is the range of data type byte in Java?
(a) -128 to 127
(b) -32768 to 32767
(c) -2147483648 to 2147483647
(d) None of the mentioned
Answer : a
Explanation : Byte occupies 8 bits in memory. Its range is from -128 to 127.
3. Which of the following are legal lines of Java code?
1. int w = (int)888.8;
2. byte x = (byte)100L;
3. long y = (byte)100;
4. byte z = (byte)100L;
(a) 1 and 2
(b) 2 and 3
(c) 3 and 4
(d) All statements are correct.
Answer : d
Explanation : Statements (1), (2), (3), and (4) are correct. (1) is correct because when
a floating-point number (a double in this case) is cast to an int, it simply loses the
digits after the decimal.(2) and (4) are correct because a long can be cast into a byte.
If the long is over 127, it loses its most significant (leftmost) bits.(3) actually works,
even though a cast is not necessary, because a long can store a byte.
4. An expression involving byte, int, and literal numbers is promoted to which of these?
(a) int
(b) long
(c) byte
(d) float
Answer : a
Explanation : An expression involving bytes, ints, shorts, literal numbers, the entire
expression is promoted to int before any calculation is done.
5. Which of these literals can be contained in a data type float variable?
(a) 1.7e-308
(b) 3.4e-038
(c) 1.7e+308
(d) 3.4e-050
Answer : b
Explanation : Range of data type float is 3.4e-038 to 3.4e+308.
Unit IV | 4.1PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
6. Which data type value is returned by all transcendental math functions?
(a)
int
(b)
float
(c)
double
(d)
long
Answer : c
Explanation : None.
7. What is the output of this program?
class average {
public static void main(String args[])
{
double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
double result;
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
System.out.print(result/6);
}
}
(a) 16.34
(b) 16.566666644
(c) 16.46666666666667
(d) 16.46666666666666
Answer : c
Explanation : None.
Output :
$ javac average.java
$ java average
16.46666666666667
8. What is the output of this program?
class conversion {
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
(a) 38 43
(b) 39 44
(c) 295 300
(d)
Answer : b
Unit IV | 4.2
295.04 300PRINCIPLES 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
Answer : a
Unit IV | 4.3
(d)
301.56560000PRINCIPLES 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.4PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
16. What is the output of this program?
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] + "" );
i++;
}
}
}
(a) i i i i i
(b) 0 1 2 3 4
(c) i j k l m
(d) None of the mentioned
Answer : a
Explanation : None.
Output :
$ javac array_output.java
$ java array_output
i i i i i
17. What is the output of this program?
class mainclass {
public static void main(String args[])
{
char a = 'A';
a++;
System.out.print((int)a);
}
}
(a) 66
(b) 67
(c) 65
(d) 64
Answer : a
Explanation : ASCII value of ‘A’ is 65, on using ++ operator character value
increments by one.
Output :
$ javac mainclass.java
$ java mainclass
66
Unit IV | 4.5PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
18. What is the output of this program?
class mainclass {
public static void main(String args[])
{
boolean var1 = true;
boolean var2 = false;
if (var1)
System.out.println(var1);
else
System.out.println(var2);
}
}
(a)
0
(b)
1
(c)
true
(d) false
(d) false
Answer : c
Explanation : None.
Output :
$ javac mainclass.java
$ java mainclass
true
19. What is the output of this program?
class booloperators {
public static void main(String args[])
{
boolean var1 = true;
boolean var2 = false;
System.out.println((var2 & var2));
}
}
(a)
0
(b)
1
(c)
true
Answer : d
Explanation : boolean ‘&’ operator always returns true or false. var1 is defined true
and var2 is defined false hence their ‘&’ operator result is false.
Output :
$ javac booloperators.java
$ java booloperators
false
Unit IV | 4.6PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
20. What is the output of this program?
class asciicodes {
public static void main(String args[])
{
char var1 = 'A';
char var2 = 'a';
System.out.println((int)var1 + " " + (int)var2);
}
}
(a)
162
(b)
65 97
(c)
67 95
(d) 66 98
(d) 99671246
Answer : b
Explanation : ASCII code for ‘A’ is 65 and for ‘a’ is 97.
Output :
$ javac asciicodes.java
$ java asciicodes
65 97
21. Which of these is data type long literal?
(a)
0x99fffL
(b)
ABCDEFG
(c)
0x99fffa
Answer : a
Explanation : Data type long literals are appended by an upper or lowercase L.
0x99fffL is hexadecimal long literal.
22. Which of these can be returned by the operator & ?
(a) Integer (b) Boolean
(c) Character (d) Integer or Boolean
Answer : d
Explanation : We can use binary ampersand operator on integers/chars (and it
returns an integer) or on booleans (and it returns a boolean).
23. Literals in java must be preceded by which of these?
(a)
L
(b)
l
(c)
D
(d)
L and l
Answer : d
Explanation : Data type long literals are appended by an upper or lowercase L.
24. Literal can be of which of these data types?
(a) integer
(c) boolean
Answer : d
Explanation : None
(b)
(d)
Unit IV | 4.7
float
all of the mentionedPRINCIPLES 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.8PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
(a) 0 2 4 6 8 (b) 1 2 3 4 5
(c) 0 1 2 3 4 5 6 7 8 9 (d) 1 2 3 4 5 6 7 8 9 10
Answer : b
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.
Output :
$ javac array_output.java
$ java array_output
1 2 3 4 5
28. What is the output of this program?
class variable_scope {
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System.out.print(x + " " + y);
}
System.out.println(x + " " + y);
}
}
(a) 5 6 5 6
(b) 5 6 5
(c) Runtime error
(d) Compilation error
Answer : d
Explanation : Second print statement doesn’t have access to y , scope y was limited
to the block defined after initialization of x.
Output :
$ javac variable_scope.java
Exception in thread “main” java.lang.Error : Unresolved compilation problem : y
cannot be resolved to a variable.
29. Which of these is incorrect string literal?
(a) “Hello World”
(b) “Hello\nWorld”
(c) “\”Hello World””
(d) “Helloworld”
Answer : d
Explanation : All string literals must begin and end in same line.
Unit IV | 4.9PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
30. What is the output of this program?
class dynamic_initialization {
public static void main(String args[])
{
double a, b;
a = 3.0;
b = 4.0;
double c = Math.sqrt(a * a + b * b);
System.out.println(c);
}
}
(a) 5.0 (b) 25.0
(c) 7.0 (d) Compilation Error
Answer : a
Explanation : Variable c has been dynamically initialized to square root of a * a + b *
b, during run time.
Output :
$ javac dynamic_initialization.java
$ java dynamic_initialization
5.0
31. 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
Explanation : Operator new allocates block of memory specified by the size of array,
and gives the reference of memory allocated to the array variable.
32. Which of these is an incorrect array declaration?
(a) int arr[] = new int[5]
(b) int [] arr = new int[5]
(c) int arr[] arr = new int[5]
(d) int arr[] = int [5] new
Answer : d
Explanation : Operator new must be succeeded by array type and array size.
33. What will this code print?
int arr[] = new int [5];
System.out.print(arr);
(a) 0
(b) value stored in arr[0].
(c) 00000
(d) Garbage value
Answer : d
Unit IV | 4.10PRINCIPLES 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) 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
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.11PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
Output :
$ javac array_output.java
$ java array_output
0 2 4 6 8
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.12PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
38. What is the output of this program?
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
Answer : d
Explanation : Array arr contains 10 elements. n contains 6 thus in next line n is given
value 2 printing arr[2]/2 i:e 2/2 = 1.
Output :
$ javac evaluate.java
$ java evaluate
1
39. What is the output of this program?
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
Answer : d
Explanation : None.
Output :
$ javac array_output.java
$ java array_output
i i i i i i i i i i
Unit IV | 4.13PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
40. What is the output of this program?
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
Answer : b
Explanation : None.
Output :
$ javac array_output.java
$ java array_output
9
41. Which of these selection statements test only for equality?
(a) if
(b) switch
(c) if and switch
(d) None of the mentioned
View Answer
Answer : b
Explanation : switch statements checks for equality between the controlling variable
and its constant cases.
42. Which of these are selection statements in Java?
(a) if()
(b) for()
(c) continue
(d) break
Answer : a
Explanation : continue and break are jump statements, and for is an looping
statement.
43. Which of the following loops will execute the body of loop even when condition
controlling the loop is initially false?
(a) do-while
(b) while
(c) for
(d) None of the mentioned
Answer : a
Explanation : None.
Unit IV | 4.14PRINCIPLES 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.15PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
47. What is the output of this program?
class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}
(a) 5 (b) 6
(c) 14 (d) compilation error
Answer : b
Explanation : Using comma operator , we can include more than one statement in
the initialization and iteration portion of the for loop. Therefore both ++i and j = i +
1 is executed i gets the value – 0,1,2,3,4 and j gets the values -0,1,2,3,4,5.
Output :
$ javac comma_operator.java
$ java comma_operator
6
48. What is the output of this program?
class jump_statments {
public static void main(String args[])
{
int x = 2;
int y = 0;
for ( ; y < 10; ++y) {
if (y % x == 0)
continue;
else if (y == 8)
break;
else
System.out.print(y + " ");
}
}
}
Unit IV | 4.16PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
(a) 1 3 5 7 (b) 2 4 6 8
(c) 1 3 5 7 9 (d) 1 2 3 4 5 6 7 8 9
Answer : c
Explanation : Whenever y is divisible by x remainder body of loop is skipped by
continue statement, therefore if condition y == 8 is never true as when y is 8,
remainder body of loop is skipped by continue statements of first if. Control comes
to print statement only in cases when y is odd.
Output :
$ javac jump_statments.java
$ java jump_statments
1 3 5 7 9
49. What is the output of this program?
class Output {
public static void main(String args[])
{
int x, y = 1;
x = 10;
if (x != 10 && x / 0 == 0)
System.out.println(y);
else
System.out.println(++y);
}
}
(a) 1
(b)
2
(c) Runtime error owing to division by zero in if condition.
(d) Unpredictable behavior of program.
Answer : b
Explanation : Operator short circuit and, &&, skips evaluating right hand operand if
left hand operand is false thus division by zero in if condition does not give an error.
Output :
$ javac Output.java
$ java Output
2
Unit IV | 4.17PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
50. What is the output of this program?
class Output {
public static void main(String args[])
{
int a = 5;
int b = 10;
first: {
second: {
third: {
if (a == b >> 1)
break second;
}
System.out.println(a);
}
System.out.println(b);
}
}
}
(a) 5 10
Answer : d
(b)
10 5
(c)
5
(d)
10
Explanation : b >> 1 in if returns 5 which is equal to a i:e 5, therefore body of if is
executed and block second is exited. Control goes to end of the block second
executing the last print statement, printing 10.
Output :
$ javac Output.java
$ java Output
10
51. What is the stored in the object obj in following lines of code?
box obj;
(a) Memory address of allocated memory of object.
(b) NULL
(c) Any arbitrary pointer
(d) Garbage
Answer : b
Explanation : Memory is allocated to an object using new operator. box obj; just
declares a reference to object, no memory is allocated to it hence it points to NULL.
Unit IV | 4.18PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
52. Which of these keywords is used to make a class?
(a) class (b) struct
(c) int (d) None of the mentioned
Answer : a
Explanation : None.
53. Which of the following is a valid declaration of an object of class Box?
(a) Box obj = new Box(); (b) Box obj = new Box;
(c) obj = new Box(); (d) new Box obj;
Answer : a
Explanation : None.
54. Which of these operators is used to allocate memory for an object?
(a) malloc (b) alloc
(c) new (d) give
Answer : c
Explanation : Operator new dynamically allocates memory for an object and returns
a reference to it. This reference is address in memory of the object allocated by new.
55. Which of these statement is incorrect?
(a) Every class must contain a main() method.
(b) Applets do not require a main() method at all.
(c) There can be only one main() method in a program.
(d) main() method must be made public.
Answer : a
Explanation : Every class does not need to have a main() method, there can be only
one main() method which is made public.
56. What is the output of this program?
class main_class {
public static void main(String args[])
{
int x = 9;
if (x == 9) {
int x = 8;
System.out.println(x);
}
}
}
Unit IV | 4.19PRINCIPLES 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)
Answer : b
Explanation : None.
Unit IV | 4.20
400
(d)
100PRINCIPLES 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.21PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
60. 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();
System.out.println(obj);
}
}
(a) 0
(b) 1
(c) Runtime error
(d) Garbage value
Answer : d
Explanation : Object obj of box class contains reference to the memory which was
given to its class instances. Printing obj will print the address of the memory.
Output :
$ javac mainclass.java
$ java mainclass
box@130671e
61. Which of these class is superclass of String and StringBuffer class?
(a) java.util
(b) java.lang
(c) ArrayList
(d) None of the mentioned
Answer : b
Explanation : None.
62. Which of these operators can be used to concatenate two or more String objects?
(a)
+
(b)
+=
(c)
&
(d)
||
Answer : a
Explanation : operator + is used to concatenate strings, Example String s = “i ” +
“like ” + “java”; String s contains “I like java”.
63. Which of these method of class String is used to obtain length of String object?
(a)
get()
(b)
Sizeof()
(c)
lengthof()
(d)
length()
Answer : d
Explanation : Method length() of string class is used to get the length of the object
which invoked method length().
Unit IV | 4.22PRINCIPLES 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.23PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
68. What is the output of this program?
class String_demo {
public static void main(String args[])
{
int ascii[] = { 65, 66, 67, 68};
String s = new String(ascii, 1, 3);
System.out.println(s);
}
}
(a) ABC
(b) BCD
(c) CDA
(d) ABCD
Answer : b
Explanation : ascii is an array of integers which contains ascii codes of Characters A,
B, C, D. String(ascii, 1, 3) is an constructor which initializes s with Characters
corresponding to ascii codes stored in array ascii, starting position being given by 1
and ending position by 3, Thus s stores BCD.
Output :
$ javac String_demo.java
$ java String_demo
BCD
69. 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);
String s1 = "abcd";
int len1 = s1.length();
int len2 = s.length();
System.out.println(len1 + " " + len2);
}
}
(a) 3 0
(b) 0 3
(c) 3 4
(d) 4 3
Answer : d
Explanation : None.
Output :
$ javac String_demo.java
$ java String_demo
4 3
Unit IV | 4.24PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
70. What is the output of this program?
class A {
int i;
int j;
A() {
i = 1;
j = 2;
}
}
class Output {
public static void main(String args[])
{
A obj1 = new A();
System.out.print(obj1.toString());
}
}
(a) true (b) false
(c) String associated with obj1 (d) Compilation Error
Answer : c
Explanation : toString() is method of class Object, since it is superclass of every class,
every object has this method. toString() returns the string associated with the calling
object.
Output :
$ javac Output.java
$ java Output
A@1cd2e5f
71. Which of the following is not a keyword in java?
(a) static (b) Boolean
(c) void (d) private
Answer : b
Explanation : Boolean is a class and is not a keyword.
72. Can we have two public classes in one java file?
(a)
True
(b) False
Answer : b
Explanation : No, a java file can contain only one public class.
Unit IV | 4.25PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
73. What is the default value of Long variable?
(a)
0
(b) 0.0
(c) 0L
(d) not defined
Answer : c
Explanation : Long variable has default value of 0L if defined as an instance/static
variable.
74. What is the default value of Object variable?
(a)
undefined
(b)
0
(c) null
(d) not defined
Answer : c
Explanation : Object variable has default value of null if defined as an instance/static
variable.
75. What is instance variable?
(a)
Instance variables are static variables within a class but outside any method.
(b) Instance variables are variables defined inside methods, constructors or blocks.
(c)
Instance variables are variables within a class but outside any method.
(d) None of the above.
Answer : c
Explanation : Instance variables are variables within a class but outside any method.
These variables are instantiated when the class is loaded.
76. What is JIT compiler?
(a) JIT improves the runtime performance of computer programs based on
bytecode.
(b) JIT is an application development framework.
(c)
JIT is an implementation of the Java Virtual Machine which executes Java
programs.
(d) None of the above.
Answer : a
Explanation : JIT, Just In-Time Compiler, improves the runtime performance of
computer programs based on bytecode.
77. Method Overloading is an example of
(a) Static Binding. (b) Dynamic Binding.
(c) Both of the above. (d) None of the above.
Answer : a
Explanation : Method Overloading is example of static binding.
Unit IV | 4.26PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
78. What invokes a thread's run() method?
(a)
JVM invokes the thread's run() method when the thread is initially executed.
(b) Main application running the thread.
(c)
start() method of the thread class.
(d) None of the above.
Answer : a
Explanation : After a thread is started, via its start() method of the Thread class, the
JVM invokes the thread's run() method when the thread is initially executed.
79. What will happen if static modifier is removed from the signature of the main
method?
(a)
Compilation Error.
(b) RunTime Error : NoSuchMethodError.
(c)
Program will compile and run without any output.
(d) Program will compile and run to show the required output.
Answer : b
Explanation : Program throws "NoSuchMethodError" error at runtime.
80. This is the parent of Error and Exception classes.
(a)
Throwable
(b) Catchable
(c) MainError
(d) MainException
Answer : a
Explanation : Throwable is the parent of both Error and Exception class.
81. What is the return type of Constructors?
(a) int (b) float
(c) void (d) None of the mentioned
Answer : d
Explanation : Constructors does not have any return type, not even void.
82. Which keyword is used by method to refer to the object that invoked it?
(a)
import
(b)
catch
(c)
abstract
(d)
this
Answer : d
Explanation : This keyword can be used inside any method to refer to the current
object. This is always a reference to the object on which the method was invoked.
83. Which of the following is a method having same name as that of its class?
(a)
finalize
(b)
delete
(c)
class
(d)
constructor
Answer : d
Explanation : A constructor is a method that initializes an object immediately upon
creation. It has the same name as that of class in which it resides.
Unit IV | 4.27PRINCIPLES 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.28PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
(a)
100
(b)
150
(c)
JAVA AS OBJECT ORIENTED ...
200
(d) 250
(d) 1
Answer : b
Explanation : None.
Output :
$ javac constructor_output.java
$ java constructor_output
150
87. What is the output of this program?
class equality {
int x;
int y;
boolean isequal() {
return(x == y);
}
}
class Output {
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal);
}
}
(a)
false
(b)
true
(c)
Answer : b
Explanation : None.
Output :
$ javac Output.java
$ java Output
true
88. What is the output of this program?
class box {
int width;
int height;
int length;
Unit IV | 4.29
0PRINCIPLES 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.30PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.)
JAVA AS OBJECT ORIENTED ...
90. What is the output of this program?
class area {
int width;
int length;
int area;
void area(int width, int length) {
this.width = width;
this.length = length;
}
}
class Output {
public static void main(String args[])
{
area obj = new area();
obj.area(5 , 6);
System.out.println(obj.length + " " + obj.width);
}
}
(a) 0 0
(b) 5 6
(c) 6 5
(d) 5 5
Answer : c
Explanation : This keyword can be used inside any method to refer to the current
object. this is always a reference to the object on which the method was invoked.
Output :
$ javac Output.java
$ java Output
6 5
91. Java Programming was designed by _____.
(a) Microsoft
(b) Mozilla Corporation
(c) Sun Microsystems
(d) Amazon Inc.
Answer : c
92. The beginning, Java was created in order to -
(a) Connect many household machines
(b) Perform Operations on the Internet
(c) Create Strong Programming alternative to C++
(d) Create high performance OS
Answer : a
Explanation : Basic aim of java was to solve the problem of connecting many
household machines together.
1
Multiple Choice Questions Unit 3&4
1. Who is considered as the creator of JAVA ?
a) Dennis Richie b) Ken Thompson c) James Gosling d) Bjarne Stroupstrup
Answer : c
2. Which of the following statements about the Java language is true?
a) Java supports only Procedural Oriented Programming approach
b) Both Procedural and Object Oriented Programming are supported in Java
c) Java supports only Object Oriented Programming approach
d) None of the Above
Answer : c
3. JVM stands for?
a) Java Verified Machine
c) Java Very large Machine
b) Java Virtual Machine
d) Java Very small machine
Answer : b
3. JRE stands for
a. Java Realtime Environment
c) Java Runtime Environment
Answer : c
b) Java Rapid Enterprise
d) None of the above
4. Java source files are compiled and converted to
a. Object code
b) machine code
file
c) Bytecode
d) executable
Answer : c
5. JVM is ___________for bytecode.
a. a compiler
b) an interpreter
above
c) assembler d) none of the
Answer : b
6. What is the size of int data type in java?
a) 1 bytes
b) 2 bytes
Answer : c
c) 4 bytes
d) 8 bytes2
7. Which is a valid float literal?
a) 1.23
b) 2
c) 1.23d
d) 1.23f
Answer : d
8. 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
9. Which of these coding types is used for data type char in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE d) None of the mentioned
Answer : c
10. Which of these values can a boolean variable contain?
a) True & False b) 0 & 1
c) Any integer value.
d) Both a & b
Answer : a
11. Which one of the following is a valid identifier in java?
a) x1
b) 1x c) $x d) x 1
Answer : a
12. Which one is the short circuit AND operator?
a) &
b) &&
c) |
Answer : b
d) ||
13. Which operator is used to implement unsigned right shift of an integer?
a. <<
b) >>
c) <<<
d) >>>
Answer : d
14. Which one of the following is a jump statement in java?
a) goto b) jump
c) break
d) if
Answer : c
15. 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
16. What is the output of this program?
class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);3
}
}
a) 25
Answer : c
b) 24
c) 32
d) 33
17. Which of the following loops will execute the body of loop even when condition
controlling the loop is initially false?
a) do-while b) while
c) for d) None of the mentioned
Answer : a
18. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type.
b) The destination type is larger than source type.
c) The destination type can be larger or smaller than source type.
d) None of the mentioned
Answer : b
19. What is the error in this code?
byte b = 50;
b = b * 50;
a) b can not store the result 2500, limited by its range.
b) * operator has converted b * 50 into int, which can not be converted to byte
without casting.
c) b can not contain value 50.
d) No error in this code
Answer : b
20. Which of these is an incorrect array declaration?
a) int arr[] = new int[5];
c) int arr[];
arr = new int[5];
b) int [] arr = new int[5];
d) int arr[] = int [5] new
Answer : d
21. Which of these selection statements test only for equality?
a) if
b) switch
c) Both a & b
d) None of the mentioned
Answer : b
22. Which of these are selection statements in Java?
a) if
b) for
c) continue
Answer : a
d) all of these4
23. 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
Answer : d
24. What is the value of the expression 2 & 3 ?
a) 2
Answer : a
b) 3
c) 6
d) 5
25. The bitwise XOR operator is denoted by
a. &
b) |
c) ~
d) ^
Answer : d
26. What is the value of the expression 8 << 2 ?
a) 2
Answer : b
b) 32
c)1 6
d) 5
27. The keyword used to create a constant variable
a) const
b) static
c) final
d) none of these
Answer : c
28. What is stored in the object obj in following lines of code? box obj;
a) Memory address of allocated memory of object.
c) Any arbitrary pointer
Answer : b
29 The Java language is:
a.
b.
c.
d.
Interpreted at run time.
Compiled to obtain executable target files.
Designed for recursive string processing.
Designed for matrix algorithm processing.
30 The Java language is designed to:
a. Run on a simulated Java machine.
b) NULL
d) Garbage5
b. Run directly on Intel microprocessors.
c. Designed for RISC computing architectures.
d. Designed for CISC computing architectures.
31 The Java language will run:
a.
b.
c.
d.
On any computer where the Java simulated machine is installed.
On any computer attached to the Internet.
On any computer with a Web browser installed.
On any computer which will also run C++.
32 The Java language provides the programmer with:
a.
b.
c.
d.
No ability to access or manipulate memory.
Protected access to machine memory.
Dynamically created data structures linked with pointers.
Access to the memory of a local machine through a network.
33 The Java language is:
a.
b.
c.
d.
An object oriented programming language.
A structured programming language.
A procedural programming language.
A stack oriented programming language.
34 The Java language was designed:
a.
b.
c.
d.
To create event driven programs with graphical interfaces.
To unify all different approaches to object orientation.
To provide a learning tool for beginning programmers.
To force programmers to implement all needed program elements
themselves.
35 The Java language has most in common with the following language:
a.
b.
c.
d.
C#.
C.
C++.
Basic.
36 The basis for object oriented programming can be found in which of the
following?
a. All of the items listed.
b. It is an outgrowth of the availability of powerful hardware.
c. It is useful in the development of graphical user interfaces.6
d. It solved some of the problems associated with structured programming.
37 That unit of code in Java that contains the specifications for objects is:
a.
b.
c.
d.
A class.
A method.
An instance variable.
A constructor.
38 Which of the following is true?
a. An object receives copies of all instance variables, but shares method
code.
b. An object receives copies of all instance variables and all methods.
c. An object receives copies of all methods, but shares instance variables.
d. An object shares both instance variables and methods with other objects.
39 Encapsulation refers to the idea that:
a.
b.
c.
d.
Instance variables can only be accessed by means of methods.
Methods can only be accessed by means of instance variables.
All other methods must be defined within the main() method.
Instance variables can only be declared within methods.
40 In order to use objects of a given class the programmer needs to:
a.
b.
c.
d.
Know what the methods do, but not how they do it.
Know all of the instance variables of the class.
Know how the method algorithms were implemented.
Know the types of all of the instance variables of the class.
41 Java programs have to be saved with the following extension:
a.
b.
c.
d.
.java
.txt
.doc
.xls
42 In Java programs, the name of the class has to:
a. Be the same as the name of the file it is saved in.
b. Be different from the name of the file it is saved in.
c. Be all capital letters.
d. Be all small letters.
43 Which of these method of class String is used to obtain length of String object?
a) get()7
b) Sizeof()
c) lengthof()
d) length()
Answer: D
44 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
45 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
46 Which of these is an oncorrect 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
47 Which of these classes is used for input and output operation when working with
bytes?
a) InputStream
b) Reader
c) Writer
d) All of the mentioned
Answer:a
48.
Consider
int a = 6; int b =
12; while(a<b){
System.out.println("In the loop");
a+=2;
b-=2;8
}
49 How many times is the phrase "In the loop" printed?
A.
B.
C.
D.
E.
1
2
3
4
5
50. In Java, elements of an array are automatically initialized to some default
value.
What is the default value for the elements of an array of integers?
A.
B.
C.
D.
E.
0
"0"
null
1
'0'
51. You want to initialize all of the elements of a double array a to the same
value equal to 1.5.
What could you write?
Assume that the array has been correctly initialized.
A. for(int i=1; i<a.length; i++) a[i] = 1.5;
B. for(int i=0; i<=a.length; i++) a[i] = 1.5;
C. for(int i=0; i<a.length; i++) a[i] = 1.5;
D. for(int i=0; i<a.length+1; i++) a[i] = 1.5;
E. for(int i=0; i<a.length-1; i++) a[i] = 1.5;
52.
Which of the following while statements is equivalent to
do{
y=x+7;9
x++;
}while(x<9);
(You might want to draw a flow chart to figure out the answer)
A. y=x+7; x++;
while(x<9){
y=x+7;
x++;
}
B. while(x<9){
y=x+7;
x++;
} y=x+7;
x++;
C. while(x<=9){
y=x+7;
x++;
}
D. A and B
E. A, B and C
53. Consider the code
int[] x = {5,6,7,8,9};
int[] y = x;
y[2] = 10;
What is the value of x[2]?
A. 6
B. 7
C. 10
D. 8
E. 010
54.
Consider the following piece of code:
int i;
for(i=0; i<10; i++){
/* some code that doesn't modify i */
}
System.out.println("i="+i);/* line A */
55 What is printed by the statement on line A?
A.
B.
C.
D.
E.
i=0
i=9
i=10
i=11
This piece of code doesn't compile since the scope of i is limited
to the for loop.
56 What is printed by the following code fragment? int[]
a = {0,1,2,3,4,5,6}; System.out.println(a.length);
A.
B.
C.
D.
E.
5
6
7
8
Can't tell. There is not enough information.
57 How would you declare and initialize an array, palette, of Color objects
of size 6?
A. Color palette = new Color[5];
B. Color palette[6];
C. Color[5] palette;
D. Color[] palette = new Color[6];
E. Color[] palette = new Color[5];
58 The documentation of a class lists a method castToIntArray that has a double
array as a formal parameter and returns an array of integers. What is the
signature of the method?11
A.
B.
C.
D.
E.
public void castToIntArray(double[]a)
public [] castToIntArray(double a)
public int castToIntArray(doublea)
public int[] castToIntArray(double[]a)
public int[] castToIntArray()
59 Which of the following is true regarding the continue statement in a for loop?
A.
B.
C.
D.
E.
continue transfers the control flow to the initialization statement of the for loop.
continue transfers the control flow to the conditional statement of the for loop.
continue transfers the control flow to the update statement of the for loop.
continue transfers the control flow to the statement just after the for loop
continue transfers the control flow to the statement just before the for loop
1.
60. ___________ is the ability of an Java application to perform multiple tasks at the
same time.
A) Multiprogramming
B) Multithreading
C) Multiprocessing
D) Multitasking
61. Java is designed for the distributed environment of the Internet, because it handles
_________ protocols
A) UDP
B) TCP/IP
C) FTP
D) TEL
62. Java is associated with _______ programming Language
A) Oak
B) Basic
C) C
D) Perl
63.Which of the following is Java keyword?
A) external
B) implement12
C) throw
D) Integer
64. A compiler converts the Java program into an intermediate language representation
called
A)bytecode
B)byte
C)byteclass
D)bytejava
65. _____________ Operator is used to create an object.
A) class
B) new
C) print
D) main
66. Which of the following method is used to initialize the instance variable of a class.
A) Class
B) Public
C) Constructor
D) Destructor _
67. Java platform consists of JVM and a package of readymade software components this
package is known as ___________.
A) Java API
B) JVM API
C) JM API
D) API
68. Which of the following is not a Looping statement ____________
A)FOR
B)Switch
C)while
D)Do-While
69. _____________ is a multi way branch statement
A) Switch
B) continue
C) break
D) Label
70. Choose the proper operator precedence when evaluating an Expression?
1. Comparisons
2. Logical operations
3. Increment13
4. Arithmetic operations
A) 1-2-4-3
B) 4-3-1-2
C) 2-1-4-3
D) 3-4-1-2
71. The ______ statement is used inside the switch to terminate a Statement sequence
A) break
B) Jump
C) exit
D) goto
1)
A package is a collection of
(a)
Classes (b) Interfaces (c) Editing tools
(d) Classes and interfaces (e) Editing tools and interfaces.
(d) Classes and interfaces (e) Editing tools and interfaces.
Answer)
d
2)
The concept of multiple inheritances is implemented in Java by
I.
Extending two or more classes.
II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
II. Extending one class and implementing one or more interfaces.
III. Implementing two or more interfaces.
(a)
Only (II) (b) (I) and (II) (c) (II) and (III)
(d) Only (I) (e) Only (III).
(d) Only (I) (e) Only (III).
Answer)
c
3)
A package is a collection of
(a)
Classes (b) Interfaces (c) Editing tools
(d) Classes and interfaces (e) Editing tools and interfaces.
(d) Classes and interfaces (e) Editing tools and interfaces.
Answer)
d
4)
In object-oriented programming, new classes can be defined by
extending existing classes. This is an example of:
(a)
Encapsulation (b) Interface (c) Composition
(d) Inheritance (e) Aggregation.
(d) Inheritance (e) Aggregation.
Answer)
d
5)
What is garbage collection in the context of Java?
(a)
The operating system periodically deletes all of the java files
available on the system.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that doesn’t make sense.
(e) When all references to an object are gone the memory used by the object is not reclaimed.
(b) Any package imported in a program and not used is automatically deleted.
(c) When all references to an object are gone, the memory used by the object is automatically reclaimed.
(d) The JVM checks the output of any Java program and deletes anything that doesn’t make sense.
(e) When all references to an object are gone the memory used by the object is not reclaimed.
Answer)
c
6)
A process that involves recognizing and focusing on the important
characteristics of a situation or object is known as:
(a)
Encapsulation (b) Polymorphism
(c) Abstraction (d) Inheritance
(e) Object persistence.
(c) Abstraction (d) Inheritance
(e) Object persistence.
Answer)
c
7)Java
is developed by__________
Options
- Patrick Naughton
- James Gosling
- both a and b
- none of these
CORRECT ANSWER : both a and b
- Patrick Naughton
- James Gosling
- both a and b
- none of these
CORRECT ANSWER : both a and b
8)In
order to run Java program, we need to install __________application
program.
Options
- jdk
- sdk
- dbk
- all of the above
CORRECT ANSWER : jdk
- jdk
- sdk
- dbk
- all of the above
CORRECT ANSWER : jdk
9)Which
of the following operation(s) is/are required to convert the java
source code into output form.
Options
- Compilation
- interpretation
- both a and b
- none of these
CORRECT ANSWER : both a and b
- Compilation
- interpretation
- both a and b
- none of these
CORRECT ANSWER : both a and b
10)Previously
Java was known as ________.
Options
- Java only
- apple
- jdk
- oak
CORRECT ANSWER : oak
- Java only
- apple
- jdk
- oak
CORRECT ANSWER : oak
11)Java
is a _____________language.
Options
- object oriented
- procedural oriented
- system oriented
- platform oriented
CORRECT ANSWER : object oriented
- object oriented
- procedural oriented
- system oriented
- platform oriented
CORRECT ANSWER : object oriented
12)Java
_________the program first and then __________it.
1) compile
2) interpret
3) assemble
4) debug
1) compile
2) interpret
3) assemble
4) debug
Options
- 1 and 2
- 2 and 1
- 3 and 4
- 1 and 4
13)CORRECT ANSWER : 1 and 2
- 1 and 2
- 2 and 1
- 3 and 4
- 1 and 4
13)CORRECT ANSWER : 1 and 2
Explanation. Java is both compiled and interpreted language. It compiles using javac and Just In Time (JIT), and interprets using Java virtual machine (JVM). |
14)Java
is developed by___________
Options
- Microsoft
- Oracle
- Sun
- Google
CORRECT ANSWER : Sun
- Microsoft
- Oracle
- Sun
CORRECT ANSWER : Sun
15)What
is the extension of the Java program?
Options
- .java
- .class
- .doc
- .txt
CORRECT ANSWER : .java
- .java
- .class
- .doc
- .txt
CORRECT ANSWER : .java
16)What
do you mean by JRE?
Options
- java runtime environment
- java runtime exception
- java runtime execution
- none of these
CORRECT ANSWER : java runtime environment
- java runtime environment
- java runtime exception
- java runtime execution
- none of these
CORRECT ANSWER : java runtime environment
17)Is
java 100% object oriented language?
Options
- yes
- no
- can’t say
- yes
- no
- can’t say
18)What
is JDK?
Options
- java development kit
- java deployment kit
- java demo kit
- java distributed kit
CORRECT ANSWER : java development kit
- java development kit
- java deployment kit
- java demo kit
- java distributed kit
CORRECT ANSWER : java development kit
CORRECT
ANSWER : no
19)Java
Virtual Machine is platform independent.
a. True
b. False
ANSWER:
False
20) What
is the range of the int data type in java?
a. –2,147,483,648
to 2,147,483,647
b. -2,147,483,647
to 2,147,483,648
c. -32,768
to 32,767
d. -32,767
to 32,768
ANSWER:
–2,147,483,648 to 2,147,483,647
21) The
following Syntax is used for?
class Subclass-name extends Superclass-name
{
//methods and fields
}
- Published on 19 Oct 15
class Subclass-name extends Superclass-name
{
//methods and fields
}
- Published on 19 Oct 15
a. Polymorphism
b. Encapsulation
c. Inheritance
d. None
of the above
Answer:
Inheritance
22) What
is known as the classes that extend Throwable class except
RuntimeException and Error?
a. Checked
Exception
b. Unchecked
Exception
c. Error
d. None
of the above
Answer:a
23)Which
are true in case of an interface?
Options
- all type of modifiers are allowed to an interface - we instantiate an interface directly - we can mark interface as final - we can create object implementation to an interface CORRECT ANSWER : we can create object implementation to an interface
False Ans: a.
Java
Compiler and Java Interpreter.
The
Java Compiler is used for compilation and the Java Interpreter is
used for execution of the application.
Ans
: Encapsulation, Inheritance and Polymorphism are the three OOPs
Principles.
Encapsulation:
Is the Mechanism that binds together code and the data it
manipulates, and keeps both safe from outside interference and
misuse.
Inheritance:
Is
the process by which one object acquires the properties of another
object. Polymorphism:
Is a feature that allows
one interface to be used for a general class of actions.
// Looks like a comment
The statement results in a compilation error Looks like a comment
No output is displayed Ans : a.
It
must have a package statement
It must be named Test.java
It must import java.lang
It
must declare a public class named Test Ans : b
Ans : Identifiers are used for class names, method names and variable
names. An identifier may be any
descriptive sequence of upper case & lower case letters,numbers
or underscore or dollar sign and must not begin with
numbers.
Ans:
void
13)What
is the argument type of program’s main( )
method? Ans :
string
array.
14)Which
characters are as first characters of an
identifier? Ans
: A – Z, a – z, _
,$
15)What
are different
comments? Ans :
1) // -- single line comment 2) /*
--
*/
multiple line comment 3) /** --
*/ documentation
16)What
is the difference between constructor method and method?
Ans
: Constructor will be automatically invoked when an object is
created. Whereas method has to be call explicitly.
17)What
is the use of bin and lib in
JDK?
Ans
: Bin contains all tools such as javac, applet viewer, awt tool etc.,
whereas Lib contains all packages and variables.
Data
types,variables and
Arrays
Ans:
Variables are locations in memory that can hold values. Before
assigning any value to a variable, it must be declared.
Ans:
Java has three kinds of variables namely, the instance variable, the
local variable and the class variable.
Local
variables are used inside blocks as counters or in methods as
temporary variables and are used to store information needed by a
single method.
Instance variables are
used to define attributes or the state of a particular object and are
used to store information needed by multiple methods in the objects.
Class variables are global to a class and to all the instances of the
class and are useful for communicating between different objects of
all the same class or keeping track of global states.
Ans: Variables can be
declared anywhere in the method definition and can be initialized
during their declaration.They are commonly declared before usage at
the beginning of the definition.
Variables with the same data type can be declared together. Local
variables must be given a value before usage.
Ans:
Variable types can be any data type that java supports, which
includes the eight primitive data types, the name of a class or
interface and an array.
Ans: Values are assigned
to variables using the assignment operator =.
Ans:
A literal represents a value of a certain type where the type
describes how that value behaves. There are different types of
literals namely number literals, character literals,
boolean
literals, string literals,etc.
Ans: An
array is an object that stores a list of items.
Ans:
Array variable indicates the type of object that the array holds. Ex:
int arr[];
b)False Ans: a.
Ans: A
combination of characters is called as string.
b)False
Ans: a.
String [
]s:
String[
s]:
String s[ ]:
Ans : a, b and d
2
3
4
Ans : d
String integer Float Ans : a.
0 to 215
0 to
216-1
0 to
215-1
Ans. d
Ans : byte, short, int, long float, double
boolean
char
Ans : int - 0 short - 0
byte - 0 long - 0 l float - 0.0
f
double
- 0.0 d boolean - false char
- null
b)False
Ans: b.
Ans : The array
subscript expression can be used to change the values of the elements
of the array.
Ans :
If a variable is declared as final variable, then you can not change
its value. It becomes constant.
Ans :
Static variables are shared by all instances of a class.
Operators
The following are the
types of operators:
Arithmetic
operators, Assignment operators,
Increment
& Decrement operators, Logical operators,
Biwise
operators, Comparison/Relational operators and Conditional operators
b)False
Ans: a.
b)False
Ans: a.
Ans: OR(|), AND(&),
XOR(^) AND NOT(~).
Ans
: % operator is the modulo operator or reminder operator. It returns
the reminder of dividing the first operand by second operand.
5
7
9
Ans : c.
Ans : No. An object
cannot be cast to a primitive value.
Ans
: Order of precedence the order in which operators are evaluated in
expressions. Associativity determines whether an expression is
evaluated left-right or right-left.
Ans : The prefix form returns the increment or decrement operation
and returns the value of the increment or decrement operation.
The
postfix form returns the current value of all of the expression and
then performs the increment or decrement operation on that value.
The
string ""8.6" The long value 8. The String "5.453.2"
Ans : d
Ans : x = 6; y = 6
Ans : x = 6; z = 5
Control
Statements
Ans: a) Sequential
public static void main(String args[]) { int i = 20;
int j = 55; int z = 0;
z
= i < j ? i : j; // ternary operator System.out.println("The
value assigned is " + z);
}
}
What
is output of the above program? Ans: The value assigned is 20
b)False
Ans: b.
b)False
Ans: a.
b)False
Ans: b.
b)False
Ans: a.
Ans: The break keyword halts the execution of the current loop and
forces control out of the loop. The continue is similar to break,
except that instead of halting the execution of the loop, it starts
the next iteration.
b)False Ans: a.
Ans : A while statement checks at the beginning of a loop to see
whether the next loop iteration should occur.
A do statement checks at the end of a loop to see whether the next
iteration of a loop should occur. The do statement will always
execute the body of a loop at least once.
Introduction
to Classes and Methods
b)False
Ans: a.
Ans:
When an object is no longer referred to by any variable, Java
automatically reclaims memory used by that object. This is known as
garbage collection.
Ans:
Methods are functions that operate on instances of classes in which
they are defined.Objects can communicate with each other using
methods and can call methods in other classes.
Method
definition has four parts. They are name of the method, type of
object or primitive type the method returns, a list of parameters and
the body of the method.
A
method's signature is a combination of the first three parts
mentioned above.
Ans:
Calling methods are similar to calling or referring to an instance
variable. These methods are
accessed using dot notation.
Ex:
obj.methodname(param1,param2)
Ans:
getClass( ) method can be used to find out what class the belongs to.
This class is defined in the object class and is available to all
objects.
Ans:
To import a class, the import keyword should be used as shown.;
import classname;
Ans: import java .
packagename . classname (or) import java.package name.*;
Ans: A constructor is a special kind of method that determines how an
object is initialized when created.
b)False
Ans: a.
Ans:
Casting is bused to convert the value of one type to another.
b)False Ans: a.
b)False
Ans: a.
b)False Ans: b.
b)False
Ans: a.
A.The
class declaration b.The access modifiers
c.The
encapsulation of data & methods with in objects d.The use of
pointers
Ans:
a,b,c.
a.They can be applied to
both data & methods
b.They
must precede a class's data variables or methods c.They can follow a
class's data variables or methods d.They can appear in any order
e.They must be applied to data variables first and then to methods
Ans: a,b,d.
Can
this instance variable be manipulated by methods out side its class?
a.yes
b.no Ans: b.
d.The
only way to gain access to this method is by calling one of the
public class methods
Ans:
a,c.
b.Private instance variables c.Public methods
d.Private
methods Ans: b,d.
b.dot
Ans: a.
a.Each
new object has its own distinctive set of instance variables b.Each
object has a copy of the instance variables of its class
c.the instance variable
of each object are seperate from the variables of other objects
d.The instance variables of each object are stored together with the
variables of other objects Ans: a,b,c.
b.the
term void Ans: a.
b.The methods of a class
only
c.The
instance variables and methods of a
class Ans: c.
b.non-static
Ans: b.
b.The
instanceof operator c.The dot operator
Ans: c.
a.yes
b.no Ans: b.
b.classes
c.methods Ans: b.
a.A class may have only one finalize method b.Finalizers are mostly
used with simple classes c.Finalizer overloading is not allowed
Ans: a,c.
a.true b.false Ans: a.
b.false
Ans: a.
a yes
a.high
b.low Ans: a,b.
b.false
Ans: a.
Ans: null
b.false
Ans: a
b.false Ans: a
b.encoding
Ans: b,a.
Ans :
public, private, protected, default, static, trancient, volatile,
final, abstract.
Ans : public, private,
protected, default.
Methods - pass by value
Anonymous
classes Method overloading Method overriding Ans : c
Packages
and interface
Ans
:The package statement defines a name space in which classes are
stored.If you omit the package, the classes are put into the default
package.
Signature... package
pkg;
Use: * It specifies to which package the classes defined in a
file belongs to. * Package is both naming and a visibility control
mechanism.
Encapsulation
:
Encapsulation is the mechanism that binds together code and the data
it manipulates and keeps both safe from outside interference and
misuse.
Inheritance:
Inheritance is the
process by which one object acquires the properties of another
object.
Polymorphism:
Polymorphism
is a feature that allows one interface to be used for a general class
of actions. The specific action is determined by the exact nature of
actions.
Code
Blocks:
Two
or more statements which is allowed to be grouped into blocks of code
is otherwise called as Code Blocks.This is done by enclosing the
statements between opening and closing curly braces. Floating-point
numbers:
Floating-point numbers which is also known as real numbers, are used
when evaluating expressions that require fractional precision.
Unicode:
Unicode
defines a fully international character set that can represent all of
the characters found in all human languages. It is a unification of
dozens of character sets, such as Latin, Greek, Arabic and many more.
Booleans:
Java
has a simple type called boolean, for logical values. It can have
only on of two possible values, true or false.
Casting:
A
cast is simply an explicit type conversion. To create a conversion
between two incompatible types, you must use a cast.
Arrays:
An array is a group of like-typed variables that are referred to by a
common name. Arrays offer a convenient means of grouping related
information. Arrays of any type can be created and may have one or
more dimension.
Relational
Operators:
The relational operators determine the relationship that one operand
has to the other. They determine the equality and ordering.
The
secondary versions of the Boolean AND and OR operators are known as
short- circuit logical operators. It is represented by || and &&..
The
switch statement is Java’s multiway branch statement. It provides
an easy way to dispatch execution to different parts of your code
based on the value of an experession.
Jump
statements are the statements which transfer control to another part
of your program. Java Supports three jump statements: break,
continue, and return.
The
data, or variable, defined within a class are called instance
variable.
K K Wagh Institute of Engineering Education & Research
Department of Computer Engineering
A. Y.: 2018 – 2019 Semester:
II Class: SE (Computer Engg)
Course: Principle of Programming Language
Course Code: 210254
Assignment No.: MCQs of Unit I
K K Wagh Institute of Engineering Education & Research
Department of Computer Engineering
A. Y.: 2018 – 2019 Semester: II
Class: SE (Computer Engg)
Course: Principle of Programming Language
Course Code: 210254
Assignment No.: MCQs of Unit II
|
Comments
Post a Comment