Você está na página 1de 59

JAVA APTITUDE QUESTIONS

01) In order for a source code file, containing the public class Test, to successfully compile, which of the following must be true? a) It must have a package statement b) It must be named Test.java c) It must import java.lang d) It must declare a public class named Test Answer: b

02)
Which of the following declare an array of string objects? a) String[ ] s; b) String [ ]s: c) String[ s]: d) String s[ ]: Answer: a, b and d

03)
Which of the following are primitive types? a) Byte b) String c) Integer d) Float Answer: a. 04) What is the range of the char type? a) 0 to 2

16

b) 0 to 2
15

c) 0 to 2
16

-1 d) 0 to 2
15

-1 Answer: d

05)
Converting of primitive types to objects can be explicitly. a) True b) False Answer: b. 06) What is the value of 111 % 13? a) 3 b) 5 c) 7 d) 9 Answer: c.

07)
What is the result of expression 5.45 + "3,2"? a) The double value 8.6

b) The string ""8.6" c) The long value 8. d) The String "5.453.2"

Answer: d

08)
What are the values of x and y? x = 5; y = ++x; Answer: x = 6; y = 6. 09) What are the values of x and z? x = 5; z = x++; Answer: x = 6; z = 5.

10)
class conditional { 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? Answer: The value assigned is 20. 11) Which of the following statements accurately describe the use of access modifiers withi n a class definition? 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 Answer: a, b, d. 12) Which of the following statements can be used to describe a public method? a) It is accessible to all other classes in the hierarchy b) It is accessible only to subclasses of its parent class c) It represents the public interface of its class d) The only way to gain access to this method is by calling one of the public class methods Answer: a, c.

13)
Which of the following types of class members can be part of the internal part of a class?

a) Public instance variables b) Private instance variables c) Public methods

d) Private methods Answer: b, d. 14) Which of the following statements correctly describes the relation between an object and the instance variable it stores? 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 separate from the variables of other objects d) The instance variables of each object are stored together with the variables of other objects Answer: a, b, c.

15) What are the functions of the dot (.) operator? a) It enables you to access instance variables of any objects within a class b) It enables you to store values in instance variables of an object c) It is used to call object methods d) It is to create a new object Answer: a, b, c.

16) Which of the following can be referenced by this variable? a) The instance variables of a class only

b) The methods of a class only c) The instance variables and methods of a class Answer: c.

17) Which of the following operators are used in conjunction with the this and super references? a) The new operator b) The instance of operator c) The dot operator Answer: c. 18) When may a constructor be called without specifying arguments? a) When the default constructor is not called b) When the name of the constructor differs from that of the class c) When there are no constructors for the class

Answer: c.

19) Because finalize () belongs to the java.lang.Object class, it is present in all ___. a) Objects b) Classes c) Methods Answer: b. 20) Identify the true statements about finalization. a) A class may have only one finalize method b) Finalizes are mostly used with simple classes c) Finalize overloading is not allowed Answer: a, c. 21) When might your program wish to run the garbage collector? a) before it enters a compute-intense section of code b) before it enters a memory-intense section of code c) before objects are finalized d) when it knows there will be some idle time Answer: a, b, d 22) What is the output of the following program? public class Question { public static void main(String args[]) { String s1 = "abc"; String s2 = "def"; String s3 = s1.concat (s2.toUpperCase ( )); System.out.println(s1+s2+s3); } }

a) abcdefabcdef b) abcabcDEFDEF c) abcdefabcDEF d) None of the above Answer: c. 23) Which of the following methods are methods of the String class? a) delete( )

b) append( ) c) reverse( ) d) replace( ) Answer: d. 24) If youre overriding the method equals() of an object, which other method you might also consider?

Answer: hashCode() 25) Which are keywords in Java? a) Null

b) Friend c) Extends

d) Synchronized Answer: c & d

26) Can you call one constructor from another if a class has multiple constructors? Answer: Yes. Use this () syntax. 27) What is an example of polymorphism? a) Inner class b) Anonymous classes c) Method overloading d) Method overriding Answer: c 28) Which of the following methods cause the String object referenced by s to be changed? a) s.concat ( )

b) s.toUpperCase ( ) c) s.replace ( ) d) s.valueOf ( ) Answer: a & b. 29) Which of the following statements are true? a) The String class is implemented as a char array, elements are addressed using the stringname [] convention b) Strings are a primitive type in Java that overloads the + operator for concatenation c) Strings are a primitive type in Java and the StringBuffer is used as the matching wrapper type d) The size of a string can be retrieved using the length property. Answer: b. 30) Which of the following are true? a) The Class class is the superclass of the Object class. b) The Object class is final. c) The Class class can be used to load other classes. d) The ClassLoader class can be used to load other classes. Answer: c and d. 31) Which of the following methods are methods of the Math class? a) absolute ( ) b) log ( ) c) cosine ( )

d) sine ( )

Answer: b. 32) Which of the following are true about the Error and Exception classes? a) Both classes extend Throwable. b) The Error class is final and the Exception class is not. c) The Exception class is final and the Error is not. d) Both classes implement Throwable. Answer: a. 32) Which of the following are valid statements? a) Public class MyCalc extends Math b) Math.max(s); c) Math.mod (4, 10);

d) none of the above. Answer: d. 33) Which of the following are true? a) The Void class extends the Class class. b) The Float class extends the Double class. c) The System class extends the Runtime class. d) The Integer class extends the Number class. Answer: d. 34) Which of the following will output -4.0 a) System.out.println(Math.floor(-4.7)); b) System.out.println(Math.round(-4.7));

c) System.out.println(Math.ceil(-4.7)); d) System.out.println(Math.Min(-4.7)); Answer: c. 35) What will happen if you attempt to compile and run the following code? Integer ten=new Integer(10); Long nine=new Long (9); System.out.println(ten + nine); int i=1; System.out.println(i + ten); a) 19 followed by 20

b) 19 followed by 11 c) Error: Can't convert java lang Integer d) 10 followed by 1 Answer: c. 36) Which of the following statements are true? a) Byte code characters are all 16-bits. b) UTF characters are all 16-bits. c) UTF characters are all 24-bits. d) Unicode characters are all 16-bits. Answer: d.

37) Which of the following are true? a) The InputStream and OutputStream classes are byte-oriented. b) The ObjectInputStream and ObjectOutputStream do not support serialized object input and output. c) The Reader and Writer classes are character-oriented. d) The Reader and Writer classes are the preferred solution to serialized object output. Answer: a & c.

38) Which of the following are true about I/O filters? a) Filters are supported on input, but not on output. b) Filters are supported by the InputStream/OutputStream class hierarchy, but not by the Reader/Writer class hierarchy. c) Filters read from one stream and write to another. d) A filter may alter data that is read from one stream and written to another. Answer: c and d. 39) Which of the following are true? a) The Serializable interface is used to identify objects that may be written to an output stream. b) The Externalizable interface is implemented by classes that control the way in which their objects are serialized. c) The Serializable interface extends the Externalizable interface. d) The Externalizable interface extends the Serializable interface. Answer: a, b and d.

40) Which of the following are true about the File class? a) A File object can be used to change the current working directory. b) A File object can be used to access the files in the current directory. c) When a File object is created, a corresponding directory or file is created in the local file system. d) File objects are used to access files and directories on the local file system. e) File objects can be garbage collected. f) When a File object is garbage collected, the corresponding file or directory is deleted. Answer: b, d and e. 41) Which of the following can you perform using the File class? a) Change the current directory b) Return the name of the parent directory c) Delete a file

d) Find if a file contains text or binary info. Answer: b and c.

42) Which of the following is the highest class in the event-delegation model? a) java.util.EventListener b) java.util.EventObject c) java.awt.AWTEvent d) java.awt.event.AWTEvent Answer: b. 43) Which of the following components generate action events? a) Buttons

b) Labels c) Check boxes

d) Windows Answer: a. 44) Suppose that you want to have an object eh handle the TextEvent of a TextArea object t. How should you add eh as the event handler for t? a) t.addTextListener(eh); b) eh.addTextListener(t); c) addTextListener(eh.t); d) addTextListener(t,eh);

Answer: a. 45) The <PARAM> tag contains two attributes namely _________ and _______. Answer: Name, value. 46) Which of the following methods are invoked by the AWT to support paint and repaint operations? a) paint( )

b) repaint( ) c) draw( )

d) redraw( ) Answer: a. 47) Which of the following are passed as an argument to the paint( ) method? a) A Canvas object b) A Graphics object c) An Image object d) A paint object Answer: b. 48) Given the following code import java.awt.*; public class SetF extends Frame{ public static void main(String argv[]){

SetF s = new SetF(); s.setSize(300,200); s.setVisible(true); } } How could you set the frame surface color to pink

a) s.setBackground(Color.pink); b) s.setColor(PINK); c) s.Background(pink);

d) s.color=Color.pink Answer: a. 49) Which Component method is used to access a component's immediate Container? a) getVisible() b) getImmediate c) getParent() d) getContainer Answer: c. 50) What is the correct ordering for the import, class and package declarations when found in a single file? a) package, import, class b) class, import, package c) import, package, class d) package, class, import Answer : a

JAVA APTITUDE QUESTIONS


1. main() { printf("%x",-1<<4); } Answer: fff0 Explanation : -1 is internally represented as all 1's. When left shifted four times the least significant 4 bits are filled with 0's.The %x format specifier specifies that the integ er value be printed as a hexadecimal value. 2. main() { char string[]="Hello World"; display(string); } void display(char *string) { printf("%s",string); } Answer: Compiler Error : Type mismatch in redeclaration of function display Explanation : In third line, when the function display is encountered, the compiler doesn't know anything about the function display. It assumes the arguments and return typ es to be integers, (which is the default type). When it sees the actual function display, the a rguments

and type contradicts with what it has assumed previously. Hence a compile time er ror occurs. 3. main() { int c=- -2; printf("c=%d",c); } Answer: c=2; Explanation: Here unary minus (or negation) operator is used twice. Same maths rules applies, ie. minus * minus= plus. Note: However you cannot give like --2. Because -- operator can only be applied to variables as a decrement operator (eg., i--). 2 is a constant and not a variable. 4. #define int char main() { int i=65;

printf("sizeof(i)=%d",sizeof(i)); } Answer: sizeof(i)=1 Explanation: Since the #define replaces the string int by the macro char 5. main() { int i=10; i=!i>14; Printf ("i=%d",i); } Answer: i=0

Explanation: In the expression !i>14 , NOT (!) operator has more precedence than > symbol. ! is a unary logical operator. !i (!10) is 0 (not of true is false). 0>14 is fals e (zero). 6. #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p; str1=s; printf("%d",++*p + ++*str1-32); } Answer: 77

Explanation: p is pointing to character '\n'. str1 is pointing to character 'a' ++*p. "p is pointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10, which is then increm ented to 11. The value of ++*p is 11. ++*str1, str1 is pointing to 'a' that is incremented by 1 and it becomes 'b'. ASCII value of 'b' is 98. Now performing (11 + 98 32), we get 77("M"); So we get the output 77 :: "M" (Ascii is 77). 7. #include<stdio.h> main() { int a[2][2][2] = { {10,2,3,4}, {5,6,7,8} }; int *p,*q; p=&a[2][2][2]; *q=***a; printf("%d----%d",*p,*q); }

Answer: SomeGarbageValue---1 Explanation: p=&a[2][2][2] you declare only two 2D arrays, but you are trying to access the third 2D(which you are not declared) it will print garbage values. *q=***a start ing address of a is assigned integer pointer. Now q is pointing to starting address of a. I f you print *q, it will print first element of 3D array. 8. #include<stdio.h> main() { struct xx { int x=3; char name[]="hello"; }; struct xx *s; printf("%d",s->x); printf("%s",s->name); } Answer: Compiler Error Explanation: You should not initialize variables in declaration 9. #include<stdio.h> main() { struct xx { int x; struct yy { char s;

struct xx *p; }; struct yy *q; }; } Answer: Compiler Error Explanation: The structure yy is nested within structure xx. Hence, the elements are of yy are to be accessed through the instance of structure xx, which needs an instance of yy to be known. If the instance is created after defining the structure the compiler will not k now about the instance relative to xx. Hence for nested structure yy you have to declare memb er. 10. main() { printf("\nab");

printf("\bsi"); printf("\rha"); } Answer: hai Explanation: \n - newline \b - backspace \r - linefeed 11. main() { int i=5; printf("%d%d%d%d%d%d",i++,i--,++i,--i,i); } Answer: 45545 Explanation: The arguments in a function call are pushed into the stack from left to right. The evaluation is by popping out from the stack. and the evaluation is from right t o left, hence the result. 12. #define square(x) x*x main() { int i; i = 64/square(4); printf("%d",i); } Answer: 64 Explanation: the macro call square(4) will substituted by 4*4 so the expression becomes i = 64/4*4 . Since / and * has equal priority the expression will be evaluated as (64/4)* 4 i.e. 16*4

= 64 13. main() { char *p="hai friends",*p1; p1=p; while(*p!='\0') ++*p++; printf("%s %s",p,p1); } Answer: ibj!gsjfoet Explanation: ++*p++ will be parse in the given order *p that is value at the location currently pointed by p will be taken ++*p the retrieved value will be incremented when ; is encountered the location will be incremented that is p++ will be execute d

Hence, in the while loop initial value pointed by p is h, which is changed to i by executing ++*p and pointer moves to point, a which is similarly changed to b and so on. S imilarly blank space is converted to !. Thus, we obtain value in p becomes ibj!gsjfoet an d since p reaches \0 and p1 points to p thus p1doesnot print anything. 14. #include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); } Answer: 50 Explanation: The preprocessor directives can be redefined anywhere in the program. So the most recently assigned value will be taken. 15. #define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); } Answer: 100 Explanation: Preprocessor executes as a seperate pass before the execution of the compiler. So textual replacement of clrscr() to 100 occurs.The input program to compiler loo ks like this : main() {

100; printf("%d\n",100); } Note: 100; is an executable statement but with no action. So it doesn't give any problem 16. main() { printf("%p",main); } Answer: Some address will be printed. Explanation: Function names are just addresses (just like array names are addresses). main() is also a function. So the address of function main will be printed. %p in pri ntf specifies that the argument is an address. They are printed as hexadecimal numbers .

27) main() { clrscr(); } clrscr(); Answer: No output/error Explanation: The first clrscr() occurs inside a function. So it becomes a function call. In the seco nd clrscr(); is a function declaration (because it is not inside any function). 28) enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); } Answer: 0..1..2 Explanation: enum assigns numbers starting from 0, if not explicitly defined. 29) void main() { char far *farther,*farthest; printf("%d..%d",sizeof(farther),sizeof(farthest)); } Answer: 4..2

Explanation: the second pointer is of char type and not a far pointer 30) main() { int i=400,j=300; printf("%d..%d"); } Answer: 400..300 Explanation: printf takes the values of the first two assignments of the program. Any number of printf's may be given. All of them take only the first two values. If more number of assign ments given in the program,then printf will take garbage values. 31) main()

{ char *p; p="Hello"; printf("%c\n",*&*p); } Answer: H Explanation: * is a dereference operator & is a reference operator. They can be applied any nu mber of times provided it is meaningful. Here p points to the first character in the string " Hello". *p dereferences it and so its value is H. Again & references it to an address and * der eferences it to the value H. 32) main() { int i=1; while (i<=5) { printf("%d",i); if (i>2) goto here; i++; } } fun() { here: printf("PP"); } Answer: Compiler error: Undefined label 'here' in function main Explanation: Labels have functions scope, in other words The scope of the labels is limited to fu nctions .

The label 'here' is available in function fun() Hence it is not visible in function mai n. 33) main() { static char names[5][20]={"pascal","ada","cobol","fortran","perl"}; int i; char *t; t=names[3]; names[3]=names[4]; names[4]=t; for (i=0;i<=4;i++) printf("%s",names[i]); } Answer: Compiler error: Lvalue required in function main Explanation: Array names are pointer constants. So it cannot be modified.

34) void main() { int i=5; printf("%d",i++ + ++i); } Answer: Output Cannot be predicted exactly. Explanation: Side effects are involved in the evaluation of i 35) void main() { int i=5; printf("%d",i+++++i); } Answer: Compiler Error Explanation: The expression i+++++i is parsed as i ++ ++ + i which is an illegal combination of operators. 36) #include<stdio.h> main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } Answer:

Compiler Error: Constant expression required in function main. Explanation: The case statement can have only constant expressions (this implies that we cannot use variable names directly so an error). Note: Enumerated types can be used in case statements. 37) main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here } Answer: 1 Explanation:

Scanf returns number of items successfully read and not 1/0. Here 10 is given as in put which should have been scanned successfully. So number of items read is 1. 38) #define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); } Answer: 100 39) main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); } Answer: 1 Explanation: before entering into the for loop the checking condition is "evaluated". Here it eval uates to 0 (false) and comes out of the loop, and i is incremented (note the semicolon after th e for loop). 40) #include<stdio.h> main() { char s[]={'a','b','c','\n','c','\0'}; char *p,*str,*str1; p=&s[3]; str=p;

str1=s; printf("%d",++*p + ++*str1-32); } Answer: M Explanation: p is pointing to character '\n'.str1 is pointing to character 'a' ++*p meAnswer:"p is p ointing to '\n' and that is incremented by one." the ASCII value of '\n' is 10. then it is increme nted to 11. the value of ++*p is 11. ++*str1 meAnswer:"str1 is pointing to 'a' that is increment ed by 1 and it becomes 'b'. ASCII value of 'b' is 98. both 11 and 98 is added and result is su btracted from 32. i.e. (11+98-32)=77("M"); 41) #include<stdio.h> main() { struct xx {

int x=3; char name[]="hello"; }; struct xx *s=malloc(sizeof(struct xx)); printf("%d",s->x); printf("%s",s->name); } Answer: Compiler Error Explanation: Initialization should not be done for structure members inside the structure declarat ion 42) #include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; } Answer: Compiler Error Explanation: in the end of nested structure yy a member have to be declared. 43) main() { extern int i; i=20;

printf("%d",sizeof(i)); } Answer: Linker error: undefined symbol '_i'. Explanation: extern declaration specifies that the variable i is defined somewhere else. The com piler passes the external variable to be resolved by the linker. So compiler doesn't find an error. During linking the linker searches for the definition of i. Since it is not found the linker fla gs an error. 44) main() { printf("%d", out); } int out=100; Answer:

Você também pode gostar