OOP Basics - Object oriented programming (MCQ) questions
Here, you can read OOP Basics multiple choice questions and answers with explanation.
1) Break statement in switch case - Published on 19 Oct 15
a. prevents from fallthrough
b. causes an exit from innermost loop
c. both a and b
d. none
Answer
Explanation
|
ANSWER: both a and b
Explanation: Break statement is an important statement which alters the normal flow of a program. It helps in exiting the switch case block . It is frequently used to terminate the processing of a particular case within the switch statement. Control passes to the statement that follows the terminated statement. It also prevents fallthrough which occurs in the absence of break in the switch case. Fallthrough is a situation that leads to execution of all the cases which is possible in absence of break statement
|
|
2) Fallthrough in a switch case statement - Published on 19 Oct 15
a. prevents the next case after the matching block to be executed
b. Allows the next case after the matching block to be executed
c. no fallthorugh occurs in switch case
d. none
Answer
Explanation
|
ANSWER: Allows the next case after the matching block to be executed
Explanation: When break statement is not present in the switch case code block, after executing the matching block the execution continues to execute the next case(fallthrough). Thus fallthrough is a situation which allows multiple values to match the same point without any special syntax. In practice, the break statement after execution of the required code(matching block) exits the switch block. This prevents the fallthrough. C# is a language that has made usage of break statement in coding , a mandatory. Compilation of code without break statement throws error and hence prevents fallthrough.
|
|
3) Which Java.Lang contains only static methods? - Published on 19 Oct 15
a. Math ,System
b. Number,Exception
c. There is no such class
d. none
Answer
Explanation
|
ANSWER: Math ,System
Explanation: System and Math are only two classes that do not have any method that does operation on the object of any class where they are used. Hence it has all methods as static. System class contains utility methods for handling operating system specific task and they do not operate on object instance. For example the getproperties() method of the System class gets the information about computer. The math class uses the utility methods for math operations. Such as exponential,logarithmic,random etc. All other classes have method/methods which operate on object of the class.
|
|
4) Use of preprocessor directive in OOP - Published on 19 Oct 15
a. for conditional compilation
b. for macro expansion
c. error and warning reporting
d. all the above
Answer
Explanation
|
ANSWER: all the above
Explanation: Preprocessor is a program that processes an input to produce an output which is used as another programs input. The preprocessor directives are generally invoked by the compiler to process before compilation. The preprocessor directive are capable of performing simple textual substitutions, macro expansion, conditional compilation, warning and error reporting. It begins with special character # followed by directive name. Preprocessor directive statement does not end with a semi colon. Few examples of preprocessor directives are:
#include, #define, #undef, #if, #elif, #else, #endif, #line etc. It is used in various OOP languages like C, C++, C#. It has no use in Java.
|
|
5) The keyword 'this' is used - Published on 19 Oct 15
a. As reference to current object
b. Explicit constructor invocation
c. In open recursion
d. All the above
Answer
Explanation
|
ANSWER: All the above
Explanation: In object oriented programming the keyword 'this' is used to refer to objects, classes or any other entity that is part of currently running code. Different languages use it in different ways. In some languages, 'this' is the only way to access data and methods in current object. The concept, however, is similar in all languages using 'this'.It is usually a fixed reference or pointer which refers to current object. In some languages it is used explicitly while others use lexical scoping( range of functionality) to use it implicitly to make symbols within their class visible. The dispatch semantics of 'this' that method calls on 'this'are dynamically dispatched which means that these methods can be overriden by derived classes or objects.
|
|
6) Dynamic dispatch is a feature that - Published on 19 Oct 15
a. selects which polymorphic operation to call at run time
b. selects which polymorphic operation to call at compile time
c. Both a and b
d. None
Answer
Explanation
|
ANSWER: selects which polymorphic operation to call at run time
Explanation: Dynamic dispatch also known as message passing is a process of selecting a procedure to run in response to a method call by looking for the method (function) in the table associated with the object, at run time. It distinguishes an object from a module which has fixed implementations for all instances i.e. static dispatch. Dynamic dispatch has usage in Object oriented programming languages when different classes have different implementations of same function call due to inheritance. For example there are three classes. Class X- the base class, Class Y and Class Z- the derived class and all of them have a function call- show( ), then dynamic dispatch selects which implementation of function to call at run time.
|
|