First Assessment Test Questions With Answers

Published on April 2017 | Categories: Documents | Downloads: 31 | Comments: 0 | Views: 533
of 27
Download PDF   Embed   Report

Comments

Content

Assessment Test 1 Nocetyps Technologies 1.Which of the following special symbol allowed in a variable name? A. * (asterisk) C. - (hyphen) Answer & Explanation Answer: Option D Explanation: Variable names in C are made up of letters (upper and lower case) and digits. The underscore character ("_") is also permitted. Names must not begin with a digit. Examples of valid (but not very descriptive) C variable names: => foo => Bar => BAZ => foo_bar => _foo42 => _ => QuUx B. D. | (pipeline) _ (underscore)

2. A default catch block catches A. all thrown objects B. no thrown objects C. any thrown object that has not been caught by an earlier catch block D. all thrown objects that have been caught by an earlier catch block Answer & Explanation Answer: Option C Explanation:

3.How would you round off a value from 1.66 to 2.0? A. ceil(1.66) C. roundup(1.66) Answer & Explanation Answer: Option A Explanation: /* Example for ceil() and floor() functions: */ #include<stdio.h> #include<math.h> B. D. floor(1.66) roundto(1.66)

int main() { printf("\n Result : %f" , ceil(1.44) ); printf("\n Result : %f" , ceil(1.66) ); printf("\n Result : %f" , floor(1.44) ); printf("\n Result : %f" , floor(1.66) ); return 0; } // // // // // Output: Result : Result : Result : Result : 2.000000 2.000000 1.000000 1.000000

4.How many times "Hello" is get printed? #include<stdio.h> int main() { int x; for(x=-1; x<=10; x++) { if(x < 5) continue; else break; printf("Hello"); } return 0; } A. Infinite times B. 11 times C. 0 times D. 10 times Answer & Explanation Answer: Option C

5.Which of the following is not logical operator? A. & B. C. || D. Answer & Explanation Answer: Option A Explanation: Bitwise operators: & is a Bitwise AND operator. Logical operators: && is a Logical AND operator. || is a Logical OR operator. && !

! is a NOT operator. So, '&' is not a Logical operator.

6. In mathematics and computer programming, which is the correct order of mathematical operators ? A. Addition, Subtraction, Multiplication, Division B. Division, Multiplication, Addition, Subtraction C. Multiplication, Addition, Division, Subtraction D. Addition, Division, Modulus, Subtraction Answer & Explanation Answer: Option B Explanation: Simply called as BODMAS (Brackets, Order, Division, Multiplication, Addition and Subtraction). nemonics are often used to help students remember the rules, but the rules taught by the use of acronyms can be misleading. In the United States the acronym PEMDAS is common. It stands for Parentheses, Exponents, Multiplication, Division, Addition, Subtraction. In other English speaking countries, Parentheses may be called Brackets, or symbols of inclusion and Exponentiation may be called either Indices, Powers or Orders, and since multiplication and division are of equal precedence, M and D are often interchanged, leading to such acronyms as BEDMAS, BIDMAS, BODMAS, BERDMAS, PERDMAS, and BPODMAS.

7.Which of the following cannot be checked in a switch-case statement? A. Character C. Float D. Answer & Explanation Answer: Option C Explanation: The switch/case statement in the c language is defined by the language specification to use an int value, so you can not use a float value. switch( expression ) { case constant-expression1: case constant-expression2: case constant-expression3: ... ... default : statements 4; } B. enum Integer

statements 1; statements 2; statements3 ;

The value of the 'expression' in a switch-case statement must be an integer, char, short, long. Float and double are not allowed.

8.Evaluate the following expression: 3 >6&&7>4 A. True B. Answer & Explanation Answer: Option B False

9.Point out the error, if any in the program. #include<stdio.h> int main() { int a = 10; switch(a) { } printf("This is c program."); return 0; } A. Error: No case statement specified B. Error: No default specified C. No Error D. Error: infinite loop occurs Answer & Explanation Answer: Option C Explanation: There can exists a switch statement, which has no case. 10.Point out the error, if any in the while loop. #include<stdio.h> int main() { int i=1; while() { printf("%d\n", i++); if(i>10) break; } return 0; } A. There should be a condition in the while loop B. There should be at least a semicolon in the while C. The while loop should be replaced with for loop. D. No error Answer & Explanation

Answer: Option A Explanation: The while() loop must have conditional expression or it shows "Expression syntax" error.

11.Which of the following errors would be reported by the compiler on compiling the program given below? #include<stdio.h> int main() { int a = 5; switch(a) { case 1: printf("First"); case 2: printf("Second"); case 3 + 2: printf("Third"); case 5: printf("Final"); break; } return 0; } A. There is no break statement in each case. B. Expression as in case 3 + 2 is not allowed. C. Duplicate case case 5: D. No error will be reported. Answer & Explanation Answer: Option C Explanation: Because, case 3 + 2: and case 5: have the same constant value 5. 12.Point out the error, if any in the while loop. #include<stdio.h> int main() { void fun(); int i = 1; while(i <= 5) { printf("%d\n", i); if(i>2) goto here;

} return 0; } void fun() { here: printf("It works"); } A. No Error: prints "It works" B. Error: fun() cannot be accessed C. Error: goto cannot takeover control to other function D. No error Answer & Explanation Answer: Option C Explanation: A label is used as the target of a goto statement, and that label must be within the same function as the goto statement. Syntax: goto <identifier> ; Control is unconditionally transferred to the location of a local label specified by <identifier>. 13.Point out the error, if any in the program. #include<stdio.h> int main() { int a = 10, b; b= a >=5 ? b=100: b=200; printf("%d\n", b); return 0; } A. 100 B. 200 C. Error: L value required for b D. Answer & Explanation Answer: Option C Explanation: Variable b is not assigned. It should be like: b = a >= 5 ? 100 : 200;

Garbage value

14.How will you free the memory allocated by the following program? #include<stdio.h> #include<stdlib.h> #define MAXROW 3 #define MAXCOL 4

int main() { int **p, i, j; p = (int **) malloc(MAXROW * sizeof(int*)); return 0; } A. memfree(int p); B. dealloc(p); C. malloc(p, 0); D. free(p); Answer & Explanation Answer: Option D Explanation: 15.Point out the error in the following program. #include<stdio.h> #include<stdlib.h> int main() { int *a[3]; a = (int*) malloc(sizeof(int)*3); free(a); return 0; } A. Error: unable to allocate memory B. Error: We cannot store address of allocated memory in a C. Error: unable to free memory D. No error Answer & Explanation Answer: Option B Explanation: We should store the address in a[i]

16.To expose a data member to the program, you must declare the data member in the _____ section of the class A. common B. C. public D. E. user Answer & Explanation Answer: Option C exposed unrestricted

17.A C++ program contains a function with the header int function(double d, char c). Which of the following function headers could be used within the same program? A. B. C. D. char function(double d, char c) int function(int d, char c) both (a) and (b) neither (a) nor (b)

Answer & Explanation Answer: Option B Explanation:the compiler differentiates the functions with same name based on. I) no. Of arguments. Ii) type of arguments. The return type is not a taken into account in case of function overloading. Thus (A) is ruled out.

18.The feature in object-oriented programming that allows the same operation to be carried out differently, depending on the object, is_____ A. inheritance B. C. overfunctioning D. Answer & Explanation Answer: Option B Explanation: 19.The process of extracting the relevant attributes of an object is known as A. polymorphism B. inheritance C. abstraction D. data hiding Answer & Explanation Answer: Option C Explanation: Abstraction is the process of extracting the relevant properties of an object while ignoring nonessential details. The extracted properties define a view of the object. 20. A function that is called automatically each time an object is destroyed is a A. constructor C. destroyer Answer & Explanation Answer: Option B Explanation: 1. Which of the following statements are TRUE about the .NET CLR? It provides a language-neutral development & execution environment. It ensures that an application would not be able to access memory that it is not authorized to access. It provides services to run "managed" applications. The resources are garbage collected. B. D. destructor terminator polymorphism overriding

It provides services to run "unmanaged" applications. A. Only 1 and 2 B. Only 1, 2 and 4 C. 1, 2, 3, 4 D. Only 4 and 5 E. Only 3 and 4 Answer & Explanation Answer: Option C Explanation:

2. Which of the following utilities can be used to compile managed assemblies into processor-specific native code? A. gacutil B. C. sn D. E. ildasm Answer & Explanation Answer: Option B Explanation: 3. Which of the following components of the .NET framework provide an extensible set of classes that can be used by any .NET compliant programming language? A. .NET class libraries B. Common Language Runtime C. Common Language Infrastructure D. Component Object Model E. Common Type System Answer & Explanation Answer: Option A Explanation: No answer description available for this question. Let us discuss. 4. Which of the following jobs are NOT performed by Garbage Collector? Freeing memory on the stack. Avoiding memory leaks. Freeing memory occupied by unreferenced objects. Closing unclosed database collections. Closing unclosed files. A. 1, 2, 3 B. 3, 5 C. 1, 4, 5 D. 3, 4 Answer & Explanation ngen dumpbin

Answer: Option C Explanation:

5. Which of the following constitutes the .NET Framework? ASP.NET Applications CLR Framework Class Library WinForm Applications Windows Services A. 1, 2 B. 2, 3 C. 3, 4 D. 2, 5 Answer & Explanation Answer: Option B Explanation:

6. Which of the following assemblies can be stored in Global Assembly Cache? A. Private Assemblies B. Friend Assemblies C. Shared Assemblies D. Public Assemblies E. Protected Assemblies Answer & Explanation Answer: Option C Explanation: 7. Which of the following is NOT a namespace in the .NET Framework Class Library? A. System.Process B. System.Security C. System.Threading D. System.Drawing E. System.Xml Answer & Explanation Answer: Option A

8. Which of the following statments are the correct way

to call the method Issue() defined in the code snippet given below? namespace College { namespace Lib { class Book { public void Issue() { // Implementation code } } class Journal { public void Issue() { // Implementation code } } } } 1) College.Lib.Book b = new College.Lib.Book(); b.Issue(); 2) Book b = new Book(); b.Issue(); 3) using College.Lib; Book b = new Book(); b.Issue(); 4) using College; Lib.Book b = new Lib.Book(); b.Issue(); 5) using College.Lib.Book; Book b = new Book(); b.Issue(); A. 1, 3 B. 2, 4 C. 3 D. 4, 5 Answer & Explanation Answer: Option A

9) Which of the following statements is correct about a namespace in C#.NET? A. Namespaces help us to control the visibility of the elements present in it. B. A namespace can contain a class but not another namespace. C. If not mentioned, then the name 'root' gets assigned to the namespace.

D. It is necessary to use the using statement to be able to use an element of a namespace. E. We need to organise the classes declared in Framework Class Library into different namespaces. Answer & Explanation Answer: Option A 10. Which of the following is absolutely neccessary to use a class Point present in namespace Graph stored in library? A. Use fully qualified name of the Point class. B. Use using statement before using the Point class. C. Add Reference of the library before using the Point class. D. Use using statement before using the Point class. E. Copy the library into the current project directory before using the Point class. Answer & Explanation Answer: Option C 11) 12. Code that targets the Common Language Runtime is known as A. B. C. D. E. Unmanaged Distributed Legacy Managed Code Native Code

Answer & Explanation 12) A class implements two interfaces each containing three methods. The class contains no instance data. Which of the following correctly indicate the size of the object created from this class? A. 12 bytes B. 24 bytes C. 0 byte D. 8 bytes E. 16 bytes Answer & Explanation Answer: Option B 13. Which of the following are parts of the .NET Framework? The Common Language Runtime (CLR) The Framework Class Libraries (FCL) Microsoft Published Web Services Applications deployed on IIS Mobile Applications A. Only 1, 2, 3 B. Only 1, 2

C. D. E. Answer:

Only 1, 2, 4 Only 4, 5 All of the above Option D

14 Which of the following statements are correct about the C#.NET code snippet given below? int[ , ] intMyArr = {{7, 1, 3}, {2, 9, 6}}; intMyArr represents rectangular array of 2 rows and 3 columns. intMyArr.GetUpperBound(1) will yield 2. intMyArr.Length will yield 24. intMyArr represents 1-D array of 5 integers. intMyArr.GetUpperBound(0) will yield 2. A. 1, 2 B. 2, 3 C. 2, 5 D. 1, 4 E. 3, 4 Answer & Explanation Answer: Option A 15. Which one of the following statements is correct? A. Array elements can be of integer type only. B. The rank of an Array is the total number of elements it can contain. C. The length of an Array is the number of dimensions in the Array. D. The default value of numeric array elements is zero. E. The Array elements are guaranteed to be sorted. Answer & Explanation Answer: Option D 16. A property can be declared inside a class, struct, Interface. A. True B. Answer & Explanation Answer: Option A False

17. A Student class has a property called rollNo and stu is a reference to a Student object and we want the statement stu.RollNo = 28 to fail. Which of the following options will ensure this functionality? A. Declare rollNo property B. Declare rollNo property C. Declare rollNo property D. Declare rollNo property E. None of the above Answer & Explanation Answer: Option D with with with with both only get, only get set set get and set accessors. accessor. and normal accessors. accessor.

18.Which of the following statements is correct about namespaces in C#.NET? A. Namespaces can be nested only up to level 5. B. A namespace cannot be nested. C. There is no limit on the number of levels while nesting namespaces. D. If namespaces are nested, then it is necessary to use using statement while using the elements of the inner namespace. E. Nesting of namespaces is permitted, provided all the inner namespaces are declared in the same file. Answer & Explanation Answer: Option C 19.Which of the following is NOT an Integer? A. Char B. Byte C. Integer D. Short E. Long Answer & Explanation Answer: Option A 20.Which of the following is an 8-byte Integer? A. Char B. Long C. Short D. Byte E. Integer Answer & Explanation Answer: Option B

// note write proper comments in ur programms to make it more understandable Question 1) Generate a random number (use rand())between 0 and 9 and let the user guess it. Exit when user guessed right.

answer) // Purpose: // //

Generate a random number between 0 and 9 and let the user guess it. Use a while loop. Exit when user guessed right.

#include <iostream> // <cstdlib> is needed in order to use the rand().

// For older compilers, use <stdlib.h> #include <stdlib.h> using namespace std; int main() { int magic; int guess;

// magic number // user's guess

cout << "I will come up with a magic number between 0 and 9 "; cout << "and ask you to guess it." << endl; magic = rand()%10; // get a random number between 0 and 9 cout << "Enter your guess: "; cin >> guess; while (guess != magic) // as long as guess is incorrect { if(guess > magic) { cout << "Too big! Guess again..." << endl; } else // guess is less than magic { cout << "Too small! Guess again..." << endl; } cin >> guess; } cout << "You are RIGHT!" << endl;; return 0; }

question 2) Write a program to delete n Characters from a given position in a given string. without using in built functions. answer) #include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h #include(string.h) void main() { char st[20],temp[20]; int pos,i,j,ct=0,n; clrscr(); printf("Enter the string:"); gets(st); printf("\nEntre the index position:"); scanf("%d",&pos); printf("\nEnter the no.of characters:"); scanf("%d",&n);

if(pos<=strlen(st)) { for(i=0;i<=strlen(st);i++) { if(i==pos) { for(j=0;st[i]!='\0';j++) // to store the 'st' in 'temp' from a giv pos { temp[j]=st[i]; i++; } temp[j]='\0'; i=pos; //to delete the 'n' char and after restore the temp in st. for(j=0;temp[j]!='\0';j++) { ct++; if(ct>n) { st[i]=temp[j]; i++; } } st[i]='\0'; } } printf("\n\nAfter deletion the string: %s",st); } else printf("\nSorry, we cannot delete from that position."); getch(); }

question 3)write a program to calculate total salary the inputs of the program will be 1."Basic"-Basic Salary 2) Hra% House rent allowance percentage of basic calculate monthly and annuall salary and also calculate tax for the annual salary it will be 5% of the annual salary

question 4) Write A loops. 1 2 3 5 6 7 10 11 12 program to generate the following pyramid of digits, using nested 4 8 9 13 14 15

answer #include<stdio.h> #include<conio.h> int main() { int i, j,n,k; printf("Enter Number Of rows"); scanf("%d",&n) ; int Number=1; for(i = 0; i < n; i++) { for(j = n-i; j >0; j--) { printf(" "); } for(k=0;k<=i;k++) { printf("%d",Number++); printf(" "); } printf("\n"); } getch(); return 0; }

question 5)Write a C program to merge two unsorted one dimensional arrays in descending order Write a program to merge two unsorted 1D arrays in descending order Note: Define the sizes of each array as a constant i.e. your program should run for arrays of any size. Answer #include(stdio.h) // note '<' & '>' in place of '(' & ')' // size of the array is 20 #define MAX 20 int main() { int f[MAX],s[MAX]; int m,n; // to read the size of two arrays int i,j,temp; clrscr(); printf("Enter the 1st array size(1-20) :"); scanf("%d",&m); printf("Enter the 2nd array size(1-20) :"); scanf("%d",&n); printf("\nEnter the 1st array elements:"); for(i=0; i< m; i++) scanf("%d",&f[i] ); printf("\nEnter the 2nd array elements:"); for(j=0; j< n; j++) scanf("%d", &s[j] );

for(j=0; j< n; j++) // to store 's' elements to 'f' { f[i]=s[ j]; i++; } printf("\nBefore descending, the merged array is:\n"); for(i=0; i< m+n; i++) printf("%5d",f[i] ); for(i=0; i< m+n; i++)// to arrange the 'f' elements in Descending oder { for(j=i; j< (m+n)-1; j++) { if(f[i]< f[ j+1]) { temp=f[i]; f[i]=f[ j+1]; f[ j+1]=temp; } } } printf("\nAfter descending, the merged array is:\n"); for(i=0; i< m+n; i++) printf("%5d",f[i] ); getch(); return 0; } question 6) Write a C program to Sort the list of integers using Bubble Sort

Answer #include(stdio.h) //note place '<' & '>' in place of '(' & ')' #include(conio.h) void bubble(int [ ], int); // func. declaration void main( ) { int a[10],n,i; clrscr(); printf("Enter the no. of elements u want to fill:"); scanf("%d",&n); printf("\nEnter the array elements:"); for(i=0; i< n; i++) scanf("%d",&a[i]); bubble(a,n); getch(); } // calling function

void bubble(int b[], int no) { int i,j,temp;

// called function

for(i=0; i< no-1; i++) { for(j=0; j< no-i; j++) { if(b[j]>b[j+1]) { temp=b[j]; b[j]=b[j+1]; b[j+1]=temp; } } } printf("\nAfter sorting : "); for(i=0; i< no; i++) printf("%5d",b[i] ); }

1. Which four options describe the correct default values for array elements of the types indicated? int -> 0 String -> "null" Dog -> null char -> '\u0000' float -> 0.0f boolean -> true A. 1, 2, 3, 4 C. 2, 4, 5, 6 Answer & Explanation Answer: Option B Explanation: (1), (3), (4), (5) are the correct statements. (2) is wrong because the default value for a String (and any other object reference) is null, with no quotes. (6) is wrong because the default value for boolean elements is false

B. D.

1, 3, 4, 5 3, 4, 5, 6

2.Which one of these lists contains only Java programming language keywords? A. class, if, void, long, Int, continue B. goto, instanceof, native, finally, default, throws C. try, virtual, throw, final, volatile, transient D. strictfp, constant, super, implements, do E. byte, break, assert, switch, include Answer & Explanation Answer: Option B Explanation:

All the words in option B are among the 49 Java keywords. Although goto reserved as a keyword in Java, goto is not used and has no function. Option A is wrong because the keyword for the primitive int starts with a lowercase i. Option C is wrong because "virtual" is a keyword in C++, but not Java. ption D is wrong because "constant" is not a keyword. Constants in Java are marked static and final. Option E is wrong because "include" is a keyword in C, but not in Java.

3.Which will legally declare, construct, and initialize an array? A. int [] myList B. int [] myList C. int myList [] D. int myList [] Answer & Explanation Answer: Option D Explanation: The only legal array declaration and assignment statement is Option D Option A is wrong because it initializes an int array with String literals. Option B is wrong because it use something other than curly braces for the initialization. Option C is wrong because it provides initial values for only one dimension, although the declared array is a two-dimensional array 4.Which is a valid keyword in java? A. interface C. Float D. Answer & Explanation Answer: Option A B. string unsigned = {"1", "2", "3"}; = (5, 8, 2); [] = {4,9,7,0}; = {4, 3, 7};

Explanation: interface is a valid keyword. Option B is wrong because although "String" is a class type in Java, "string" is not a keyword. Option C is wrong because "Float" is a class type. The keyword for the Java primitive is float. Option D is wrong because "unsigned" is a keyword in C/C++ but not in Java. 5.You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective? A. public B. private C. protected D. transient Answer & Explanation Answer: Option C

6.public class Outer { public void someOuterMethod() { //Line 5 } public class Inner { } public static void main(String[] argv) { Outer ot = new Outer(); //Line 10 } } Which of the following code fragments inserted, will allow to compile? A. new Inner(); //At line 5 B. new Inner(); //At line 10 C. new ot.Inner(); //At line 10 D. new Outer.Inner(); //At line 10 Answer & Explanation Answer: Option A Explanation: Option A compiles without problem. Option B gives error - non-static variable cannot be referenced from a static context. Option C package ot does not exist. Option D gives error - non-static variable cannot be referenced from a static context.

7.public class Test { } What is the prototype of the default constructor? A. Test( ) B. Test(void) C. public Test( ) D. public Test(void) Answer & Explanation Answer: Option C Explanation: Option A and B are wrong because they use the default access modifier and the access modifier for the class is public (remember, the default constructor has the same access modifier as the class). Option D is wrong. The void makes the compiler think that this is a method specification - in fact if it were a method specification the compiler would spit it out. 8.Which is the valid declarations within an interface definition? A. B. C. D. Answer public double methoda(); public final double methoda(); static void methoda(double d1); protected void methoda(double d1); & Explanation

Answer: Option A Explanation: Option A is correct. A public access modifier is acceptable. The method prototypes in an interface are all abstract by virtue of their declaration, and should not be declared abstract. Option B is wrong. The final modifier means that this method cannot be constructed in a subclass. A final method cannot be abstract. Option C is wrong. static is concerned with the class and not an instance. Option D is wrong. protected is not permitted when declaring a method of an interface. See information below. Member declarations in an interface disallow the use of some declaration modifiers; you cannot use transient, volatile, or synchronized in a member declaration in an interface. Also, you may not use the private and protected specifiers when declaring members of an interface.

9.Which one is a valid declaration of a boolean? A. boolean b1 = B. boolean b2 = C. boolean b3 = D. boolean b4 = E. boolean b5 = Answer & Explanation Answer: Option C 0; 'false'; false; Boolean.false(); no;

Explanation: A boolean can only be assigned the literal true or false. 10.Which is a valid declarations of a String? A. String s1 = null; B. String s2 = 'null'; C. String s3 = (String) 'abc'; D. String s4 = (String) '\ufeed'; Answer & Explanation Answer: Option A Explanation: Option A sets the Option B is wrong Option C is wrong quotes ('abc'). Option D is wrong (object). String reference to null. because null cannot be in single quotes. because there are multiple characters between the single because you can't cast a char (primitive) to a String

11.What is the numerical range of a char? A. -128 to 127 B. -(215) to (215) - 1 C. 0 to 32767 D. 0 to 65535 Answer & Explanation Answer: Option D Explanation: A char is really a 16-bit integer behind the scenes, so it supports 216 (from 0 to 65535) values.

12.public void foo( boolean a, boolean b) { if( a ) { System.out.println("A"); /* Line 5 */ } else if(a && b) /* Line 7 */ { System.out.println( "A && B"); } else /* Line 11 */ { if ( !b ) { System.out.println( "notB") ; } else { System.out.println( "ELSE" ) ; } } } A. If a is true and b is true then the output is "A && B" B. If a is true and b is false then the output is "notB" C. If a is false and b is true then the output is "ELSE" D. If a is false and b is false then the output is "ELSE" Answer & Explanation Answer: Option C Explanation: Option C is correct. The output is "ELSE". Only when a is false do the output lines after 11 get some chance of executing. Option A is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. The condition at line 7 will never be evaluated (when a is true it will always be trapped by the line 12 condition) therefore the output will never be "A && B". Option B is wrong. The output is "A". When a is true, irrespective of the value of b, only the line 5 output will be executed. Option D is wrong. The output is "notB". 13.switch(x) { default: System.out.println("Hello"); } Which two are acceptable types for x? byte long char float Short Long A. 1 and 3 B. 2 and 4 C. 3 and 5 D. 4 and 6 Answer & Explanation

Answer: Option A Explanation: Switch statements are based on integer expressions and since both bytes and chars can implicitly be widened to an integer, these can also be used. Also shorts can be used. Short and Long are wrapper classes and reference types can not be used as variables. 14.public void test(int x) { int odd = 1; if(odd) /* Line 4 */ { System.out.println("odd"); } else { System.out.println("even"); } } Which statement is true? A. Compilation fails. B. "odd" will always be output. C. "even" will always be output. D. "odd" will be output for odd values of x, and "even" for even values. Answer & Explanation Answer: Option A Explanation: The compiler will complain because of incompatible types (line 4), the if expects a boolean but it gets an integer. 15.public class While { public void loop() { int x= 0; while ( 1 ) /* Line 6 */ { System.out.print("x plus one is " + (x + 1)); /* Line 8 */ } } } Which statement is true? A. There is a syntax error on line 1. B. There are syntax errors on lines 1 and 6. C. There are syntax errors on lines 1, 6, and 8. D. There is a syntax error on line 6. Answer & Explanation Answer: Option D Explanation:

Using the integer 1 in the while statement, or any other looping or conditional construct for that matter, will result in a compiler error. This is old C Program syntax, not valid Java. A, B and C are incorrect because line 1 is valid (Java is case sensitive so While is a valid class name). Line 8 is also valid because an equation may be placed in a String operation as shown. 16.void start() { A a = new A(); B b = new B(); a.s(b); b = null; /* Line 5 */ a = null; /* Line 6 */ System.out.println("start completed"); /* Line 7 */ } When is the B object, created in line 3, eligible for garbage collection? A. after line 5 B. after line 6 C. after line 7 D. There is no way to be absolutely certain. Answer & Explanation Answer: Option D

17.class Bar { } class Test { Bar doBar() { Bar b = new Bar(); /* Line 6 */ return b; /* Line 7 */ } public static void main (String args[]) { Test t = new Test(); /* Line 11 */ Bar newBar = t.doBar(); /* Line 12 */ System.out.println("newBar"); newBar = new Bar(); /* Line 14 */ System.out.println("finishing"); /* Line 15 */ } } At what point is the Bar object, created on line 6, eligible for garbage collection? A. after line 12 B. after line 14 C. after line 7, when doBar() completes D. after line 15, when main() completes Answer & Explanation Answer: Option B Explanation: Option B is correct. All references to the Bar object created on line 6 are destroyed when a new reference to a new Bar object is assigned to the variable newBar on line 14. Therefore the Bar object, created on line 6, is eligible for garbage collection after line 14. Option A is wrong. This actually protects the object from garbage collection.

Option C is wrong. Because the reference in the doBar() method is returned on line 7 and is stored in newBar on line 12. This preserver the object created on line 6. Option D is wrong. Not applicable because the object is eligible for garbage collection after line 14. 18.What will be the output of the program? public class Foo { public static void main(String[] args) { try { return; } finally { System.out.println( "Finally" ); } } } A. Finally B. Compilation fails. C. The code runs with no output. D. An exception is thrown at runtime. Answer & Explanation Answer: Option A Explanation: If you put a finally block after a try and its associated catch blocks, then once execution enters the try block, the code in that finally block will definitely be executed except in the following circumstances: An exception arising in the finally block itself. The death of the thread. The use of System.exit() Turning off the power to the CPU. I suppose the last three could be classified as VM shutdown. 19.What will be the output of the program? public class X { public static void main(String [] args) { try { badMethod(); //Exception Will Be thrown System.out.print("A"); } catch (RuntimeException ex) /* Line 10 */ { System.out.print("B"); } catch (Exception ex1) {

System.out.print("C"); } finally { System.out.print("D"); } System.out.print("E"); } public static void badMethod() { throw new RuntimeException(); } } A. BD B. BCD C. BDE D. BCDE Answer & Explanation Answer: Option C Explanation: A Run time exception is thrown and caught in the catch statement on line 10. All the code after the finally statement is run because the exception has been caught. 20.public class X { public static void main(String [] args) { X x = new X(); X x2 = m1(x); /* Line 6 */ X x4 = new X(); x2 = x4; /* Line 8 */ doComplexStuff(); } static X m1(X mx) { mx = new X(); return mx; } } After line 8 runs. how many objects are eligible for garbage collection? A. 0 B. 1 C. 2 D. 3 Answer & Explanation Answer: Option B

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close