Saturday 19 November 2011

TCS ILP PAT TEST - JAVA ANSWERS and EXPLANATIONS


Please do confirm the answers,these are the answers which are correct to our perspective.

1.Which of the following statements is false about objects?
a.An instance of a class is an object
b.Objects can access both static and instance data
c.Object is the super class of all other classes
d.Objects do not permit encapsulation

EXP:
objects are clearly the instance of a class.So objects has nothing to do with super class.
Ans: C 

2.Which methods can access to private attributes of a class?
a.Only Static methods of the same class
b.Only instances of the same class
c.Only methods those defined in the same class
d.Only classes available in the same package.

Exp:
Private attributes cannot be accessed outside the class.The private keyword that means no one can access that member except that particular class, inside methods of that class. Other classes in the same package cannot access private members, so it is as if you a€™re even insulating the class against yourself.
for example :
class demo
{
int a;
public int b;
private int c; //private variable

void setc ( int k)
{c=k;}

void getc ()
{return c;}
}

class accessdemo
{
public static void main(String args[])
{
demo d1 = new demo();

d1.a=10;
d1.b=20;
//but this type of assigning is not possible with the private variable c
d1.c=30; //this is ERROR
//we can access the private variable like this,
d1.setc(30);
// it can be displayed as follows
System.out.println("a,b,c :" + d1.a +"   "+d1.b+"  "+d1.getc() );
}
}

thus from this we can know that 
Ans:C.Only methods those defined in the same class

3.What is an aggregate object?
a.An object with only primitive attributes
b.An instance of a class which has only static methods
c.An instance which has other objects
d.None of the above

EXP:An aggregate object is an object that contains other objects for the purpose of grouping those objects as a unit. It is also called a container or a collection. Examples are a linked list and a hash table.

Ans:C.An instance which has other objects

4.Assume that File is an abstract class and has to File() method. ImageFile and Binary File are concrete classes of the abstract class File. Also, assume that the method toFile() is implemented in both Binary File and Image File. A File references an ImageFile object in memory and the toFile method is called,
which implementation method will be called?

a.Binary File
b.Image File
c.Both File and Binary Files
d.None of the above

I cannot catch the question ,if anyone got the answer please comment it.thanks.

5.A class can have many methods with the same name as long as the number of
parameters or type of parameters is different. This OOP concept is known as
a.Method
b.Invocating
c.Method
d.Overriding
e.Method Labeling
f.Method
g.Overloading

EXP: you can understand it from the following example,the concept is called overloading.
class OverloadDemo { 
void test() { 
System.out.println("No parameters"); 
// Overload test for one integer parameter. 
void test(int a) { 
System.out.println("a: " + a); 
// Overload test for two integer parameters. 
void test(int a, int b) { 
System.out.println("a and b: " + a + " " + b); 
// overload test for a double parameter 
double test(double a) { 
System.out.println("double a: " + a); 
return a*a; 
class Overload { 
public static void main(String args[]) { 
OverloadDemo ob = new OverloadDemo(); 
double result; 
// call all versions of test() 
ob.test(); 
ob.test(10); 
ob.test(10, 20); 
result = ob.test(123.2); 
System.out.println("Result of ob.test(123.2): " + result); 
}

Ans: g.overloading.

6.Which of the following is considered as a blue print that defines the variables
and methods common to all of its objects of a specific kind?
a.Object
b.Class
c.Method
d.Real data
e.types

Ans: i think this question doesnt need explanation.the answer is B.class

7.What are the two parts of a value of type double?
a.Significant Digits,
b.Exponent
c.Length, Denominator
d.Mode, Numerator

Ans: significant digits and exponent  are the two parts of a value of type double.

8.After the following code fragment, what is the value in fname?
Code:
String str;
int fname;
str = "Foolish boy.";
fname = str.indexOf("fool");
a.2
b.-1
c.4

Exp:The indexOf method is used to locate a character or string within another string.It returns -1 when the string is not present.Here as it is fool (not f caps) it will return -1,so answer will be -1

Ans: B.-1

9.What is the value of ‘number’ after the following code fragment execution?
Code:
int number = 0;
int number2 = 12
while (number < number2)
{number = number + 1;
}
a.5
b.12
c.21
d.13

Exp:This is very simple,here the loop executes untill the number becomes greater or equal to number2.So now when the number value is 11 it enters the loop and now number becomes 12,now again when the condition is checked the condition is false.Hence the number value will be 12.

Ans:B.12
10.10.Given the following code snippet;
Code:
int salaries[];
int index = 0;
salaries = new int salaries[4];
while (index < 4)
{
salaries[index] = 10000;
index++;
}
What is the value of salaries [3]?
a.4000
b.5000
c.1500
d.1000

Ans:Here the index value is alone increamented hence all the values of salaries will be 10000.But there is no option stating 10000.

12.Which of the following is not a return type?
a.boolean
b.void
c.public
d.Button

Ans:d.button.

13.If result = 2 + 3 * 5, what is the value and type of ‘result’ variable?
a.17,byte
b.25, byte
c.17, int
d.25, int

Ans:C.17,int reason:priority is multiplication and then addition.

14.What is the data type for the number 9.6352?
a.float
b.double
c.Float
d.Double

Exp:i believe in executing things,so as we executed the following program,

public  class floattest
{
public static void main(String[] args)
{

float f;
f=9.6352 ;
System.out.println("F value is:"+f);
}

}


javac floattest.java
gava the result -- possible loss of precision.

Ans: B. double

15.Assume that the value 3929.92 is of type ‘float’. How to assign this value
after declaring the variable ‘interest’ of type float?
a.interest = 3929.92
b.interest = (Float)3929.92
c.interest = 3929.92(float)
d.interest = 3929.92f

working on the rest stay connected !!!!

24 comments:

  1. plz ...review ur 1 and 12 answer....
    1. As per my understandng ..Object is the base class....u can say java.lang.object..so it is super class for all classes..ans..B
    12. i think public is access modifier not return type.

    ReplyDelete
    Replies
    1. Dude
      Question 1 : Ans is D not B

      Delete
    2. +1 object provide concept of encapsulation... D is clearly a false statement,, hence answer is D

      Delete
  2. welcome rishi ,
    12.public and button both may be the answer ,coz public is an access modifier.

    ReplyDelete
  3. kumar.... u can return a button object in applets ... so public is the ans 4 sure

    ReplyDelete
  4. Answer of 4th question is B.ImageFile & BinrayFile both are concrete classes for File class it means both are extending File class.When u extend a abstract class u will have to define all methods that have already been declared in abstract class so ImageFile & BinaryFile class contain toFile() method.Now File class reference is calling ImageFile object in memory means
    File f=new ImageFile();
    Its a concept of overriding & polymorphism .U can assign n number of subclass object in superclass reference variable.In this case method of subclass will be called so ImageFile method will be called.

    ReplyDelete
  5. 13.If result = 2 + 3 * 5, what is the value and type of ‘result’ variable?
    a.17,byte
    b.25, byte
    c.17, int
    d.25, int

    Ans:C.17,int reason:priority is multiplication and then addition.

    why not...a) 17,byte..Range of byte is -128 to 127..so ans is A)17,byte..

    ReplyDelete
    Replies
    1. default number type is int

      Delete
    2. its because any integral(integer) calculation is automatically implicit type casted to int data type... even if you multiply 2 byte variable.

      Delete
  6. answer of question 4 will be
    image file

    ReplyDelete
  7. ans of first q will be d) because java.lang.Object will be superclass of all classes
    object can access both static and instance data
    so definitely d)

    ReplyDelete
  8. This comment has been removed by the author.

    ReplyDelete
  9. 14th Question:
    For float, you have to type an 'f' along with its value. Like this:
    public class floattest
    {
    public static void main(String[] args)
    {

    float f;
    f=9.6352f;
    System.out.println("F value is:"+f);
    }

    }
    This will work perfectly. And the output is:
    F value is:9.6352

    ReplyDelete
  10. 4th question
    abstract class File
    {
    public abstract void toFile();
    }


    class ImageFile extends File
    {
    public void toFile()
    {
    System.out.println("ImageFile");
    }
    }

    class BinaryFile extends File
    {
    public void toFile()
    {
    System.out.println("BinaryFile");
    }

    public static void main(String... args)
    {
    File f=new ImageFile();
    f.toFile();
    }

    }

    output: imagefile
    As the toFile() method is being overrided so preference is given to runtime object so BinaryFile toFile() method will get executed

    ReplyDelete
    Replies
    1. answer to above question is it prints content with in the function of ImageFile() because it overrides and object created to ImageFile class here not BinaryFile class just check it by running on you machine

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. i think u gave the 12th answer incorrect it should be public since Button is a class and can be used as a return type.

    ReplyDelete
  13. thanx Nirav..
    could u please eloberate the explanation for polymorphism concept inaccordance with 4th question

    ReplyDelete
  14. In ques 10. I think you have the array initialization wrong. new int salaries[size] is not how you initialize an array. It should be something like int salaries[] = new int[4]. Correct it!!!

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...