Right Place For Right PersonTM 
Sponsored Ads
Buy This Website(This website is for Sale)
Home Tutorials Articles Forums Source Code Books Certifications Interviews Questions

Previous Home Next

Sun Java Certification Program

Operator and assignments

Questions .1 Which statements about the output of the following program are true?
public class Logic {
public static void main(String args[]) {
int i = 0;
int j = 0;

boolean t = true;
boolean r;

r = (t && 0<(i+=1));
r = (t && 0<(i+=2));
r = (t && 0<(j+=1));
r = (t || 0<(j+=2));

System.out.println( i + “ ” + j );
}
}
(a) The first digit printed is 1.
(b) The first digit printed is 2.
(c) The first digit printed is 3.
(d) The second digit printed is 1.
(e) The second digit printed is 2.
(f) The second digit printed is 3.

Questions 2 Which statements about the output of the following program are true?
public static void main(String args[])
{
int i =0;
i = i++;
System.out.println(i);
}
(a) 0 is printed.
(b) 1 is printed.

Questions 3 Which statements about the output of the following program are true?
public class EqualTest {
public static void main(String args[]) {
String s1 = “YES”;
String s2 = “YES”;
if ( s1 == s2 ) System.out.println(“equal”);
String s3 = new String(“YES”);
String s4 = new String(“YES”);
if ( s3 == s4 ) System.out.println(“s3 eq s4”);
}
}
(a) “equal” is printed, “s3 eq s4” is printed.
(b) “equal” is printed only.
(c) “s3 eq s4” is printed only.
(d) Nothing is printed.

Questions 4 What happens when you try to compile and run the following code?
public class EqualsTest {
public static void main(String args[]) {
char A = ‘\u0005’;
if ( A == 0x0005L )
System.out.println(“Equal”);
else
System.out.println(“Not Equal”);
}
}
(a) The compiler reports “Invalid character in input” in line 3
(b) The program compiles and prints “Not equal”.
(c) The program compiles and prints “Equal”.

Questions 5 What will happen when you attempt to compile and run the following code?
public class As{
int i = 10;
int j;
char z= 1;
boolean b;

public static void main(String argv[]){
As a = new As();
a.amethod();
}

public void amethod(){
System.out.println(j);
System.out.println(b);
}
}
(a) Compilation succeeds and at run time an output of 0 and false.
(b) Compilation succeeds and at run time an output of 0 and true.
(c) Compile time error b is not initialised.
(d) Compile time error z must be assigned a char value.

Questions 6 Given the following code what will be the output?
class ValHold {
public int i = 10;
}

public class ObParm {
public static void main(String argv[]) {
ObParm o = new ObParm();
o.amethod();
}

public void amethod(){
int i = 99;
ValHold v = new ValHold();
v.i=30;
another(v,i);
System.out.println(v.i);
} //End of amethod

public void another(ValHold v, int i){
i = 0;
v.i = 20;
ValHold vh = new ValHold();
v = vh;
System.out.println(v.i+ " "+i);
}//End of another
}
(a) 10,0,30
(b) 20,0,30
(c) 20,99,30
(d) 10,0,20

Questions 7 Here are three proposed alternatives to be used in a method to return false if the object reference x has the null value. Which statement will work?
(a) if ( x == null ) return false;
(b) if ( x.equals(null) ) return false;
(c) if ( x instanceof null ) return false;

Questions 8 What will be the result of compiling and running the given program?
Select one correct answer.
1 class Q1
2 {
3 public static void main(String arg[])
4 {
5 int a[]={2,2};
6 int b=1;
7 a[b]=b=0;
8 System.out.println(a[0]);
9 System.out.println(a[1]);
10 }
11 }

(a) Compile time error at the line no. 5.
(b) Run time error at the line no. 5.
(c) Program compiles correctly and print 2,0 when executed.
(d) Program compiles correctly and print 0,2 when executed.

Questions 9 What will be the result of compiling and running the given program?
Select one correct answer.

1 public class Q4
2 {
3 public static void main(String[] args)
4 {
5 boolean t1 = true, t2 = false, t3 = t1;
6 boolean t = false;
7 t &&= (t1 || ( t2 && t3));
8 System.out.println(t);
9 }
10 }
(a) Program compiles correctly and print true when executed.
(b) Program compiles correctly and print false when executed.
(c) Compile time error.
(d) Run time error.

Questions 10 What will be the result of compiling and running the given program?
Select one correct answer.
1 class AA{}
2 class BB extends AA{}
3 class Q6
4 {
5 public static void main(String arg[])
6 {
7 AA a=null;
8 BB b=(BB)a;
9 System.out.println(b);
10 System.out.println(b instanceof BB);
11 System.out.println(b instanceof AA);
12 }
13 }
(a) Program compiles correctly and print null,true,false.
(b) Program compiles correctly and print null,true,true.
(c) Program compiles correctly and print null,false,false.
(d) Program compiles correctly and print null,false,true.
(e) Compile time error at line no.8 as null value cannot be casted.
(f) Run time error at line no. 8 as null value cannot be casted.

Questions 11 Which one of the following are valid character contants?
Select any two.
(a) char c = '\u000d' ;
(b) char c = '\u000a';
(c) char c = '\u0000';
(d) char c = '\uface' ;

Answers: 1. (c), (d)
Unlike & and | operators, the && and || operators short circuit the evaluation of their operands if the result of the operation can be determined just based on the value of the first operand. The second operand of the || operation in the program is never evaluated. Variable i ends up with the value 3 which is the first digit printed, and j ends up with a value of 1 which is the second digit printed.

Answers: 2 (a)
To explain u in a more simple way...

int k = 10;
int j;

Post increment :
j = k++;

In this statement the value of k is 10, & there is a ++ operator also
attached with it, so it will be assigned to j, so now j gets 10, but
k becomes as 11 because the ++ expression comes after the variable,
or if u see k as k++.

Pre-increment :
j = ++k;
In this case, k is having 10 as value, then the pre-increment
operation is being performed, so k becomes 11 in this case, & then it
is being assigned to j, so now j becomes 11 & k also becomes 11.

The thing which u have must have noticed in the post increment & pre-
increment is that the value of k is increased by 1, where as j in the
first case becomes 10 & in second case j becomes 11.

Answers: 3 (b)
The compiler creates one String object for both s1 and s2, thus “equal” appears. But using the new String operator two distinct objects are created so “s3 eq s4” does not appear.

Answers: 4 (c)
The compiler promotes variable A to a long before the comparison. The compiler does not report “Invalid character in input” in line 3 because this is correct form for initializing a char primitive. Therefore, answer (a) is incorrect. Because (c) is correct  (b) cannot possibly be correct.

Answers: 5 (a)
The default value for a boolean declared at class level is false, and integer is 0, and 1 is within the range of the char and don’t need casting.

Answers: 6 (d)
In the call another(v,i);
A COPY of the reference to v is passed and thus any changes will be intact after this call.

Answers: 7 (a)
The ONLY correct way to check a reference for the null value. Answer (b) is incorrect because if x is null, there will not be an object whose equals method can be called. This statement would cause a NullPointerException at runtime when x is null. Answer (c) is incorrect because only a reference type, such as a class, or array, can be the right operand of the instanceof operator.

Answers: 8 (c)
First of all value of b which is 1 is assigned to array index then rest of the code will be executed.

Answers: 9 (c)
Compile time error: There is no operator like &&=

Answers: 10 (c)
As null value can be casted but do remember it is of no use to cast null value that is why it is written in Khalid Mughal's book that we can not cast null value.Secondly,instanceof operator always return false for null value.

Answers: 11 (c), (d)
'\u000d' and '\u000a' are not valid as these value do line break. Note:'\u000a' is not valid even when used behind single line comments (i.e. //.....),but offcourse for multiline comment it is valid. '\uface' is valid because we can use any character from range 0 - 9 or A - F or a - f.

Previous Home Next
Sponsored Ads
Sponsored Ads
Sponsored Ads
Sponsored Ads
Sponsored Ads
Interview Questions And Answers
Struts interview questions and answers (Subjective)
500 Java Objective Questions and Answer
Core Java Objective Questions And Answers
Core Java Subjective Questions And Answers
Core Java Interview Questions And Answers
Core Java Interview Questions and Answers (Subjective)
Core Java Interview Questions and Answers( Objective)
50 Servlet interview questions
155 Java Interview Questions
EJB Interview Questions and Answers(Subjective)
R4R,JSP Interview Questions and Answer(Subjective)
R4R,Java Servlets Interview Questions and Answers(Subjective)
Core Java Subjective ,Objective and Interview Questions And Answers
275 Core java interview questions
Java Objective Questions and Answer
Java Architect Interview Questions
Applet Interview Questions and Answers
Core Java example
Servlet Objective Questions And Answers
Servlet Subjective Questions And Answers
Servlet Interview Questions And Answers
JSP Objective Questions And Answers
JSP Subjective Questions And Answers
JSP Interview Questions And Answers
JDBC Objective Questions And Answers
JDBC Subjective Questions And Answers
JDBC Interview Questions And Answers
JDBC Interview questions and answers
Networking Interview questions and answers
Servlets Interview questions and answers
EJB Objective Questions And Answers
EJB Subjective Questions And Answers
EJB Interview Questions And Answers
Hibernate Objective Questions And Answers
Hibernate Subjective Questions And Answers
Hibernate Interview Questions And Answers
Spring Objective Questions And Answers
Spring Subjective Questions And Answers
Spring Interview Questions And Answers
Struts Objective Questions And Answers
Struts Subjective Questions And Answers
Struts Interview Questions And Answers
Ant Objective Questions And Answers
Ant Subjective Questions And Answers
Ant Interview Questions And Answers
PHP Objective Questions And Answers
PHP Subjective Questions And Answers
PHP Interview Questions And Answers 1
ASP.net Objective Questions And Answers
ASP.net Subjective Questions And Answers
ASP.net Interview Questions And Answers 1
ASP.net interview questions ,ASP.net interview answers,ASP.net interview questions and answers
PHP interview questions ,PHP interview answers,PHP interview questions and answers
Testing Objective Questions And Answers
Testing Subjective Questions And Answers
Testing Interview Questions And Answers
Ajax Tutorials
Ajax Objective Questions And Answers
Ajax Subjective Questions And Answers
Ajax Interview Questions And Answers
Linux Objective Questions And Answers
Linux Subjective Questions And Answers
Unix Subjective Questions And Answers
Unix Interview Questions And Answers
HR interview questions and answers
HR Interview Questions Tips
HR Objective Questions And Answers
HR Subjective Questions And Answers
HR Interview Questions And Answers
Learn C Language with in a day
 C /C++ Questions
C Objective Questions And Answers
C Subjective Questions And Answers
C Interview Questions And Answers
C Objective Interview Questions And Answers(10)
C Subjective Interview Questions And Answers(100) 
C syntax,semantics and simple programming questions(61) 

Linux Interview Questions And Answers
Unix Objective Questions And Answers
C Aptitude Questions(179)
C++ Interview Questions And Answers    
C++ Objective Questions And Answers
C++ Subjective Questions And Answers
C++ Interview Questions And Answers
C,C++ objective Interview Questions and answers
C Interview Questions And Answers( Objective and Subjective)
C Objective Interview Questions And Answers(10)

C Subjective Interview Questions And Answers(100)

C,C++ Interview Questions

35 C++ Interview Questions And Answers(Subjective)

109 C++ Interview Questions

Java Script Interview Question and Answer(1-10).
 Java Script Interview Question and Answer(11-20).
Java Script  Interview Question and Answer(21-30).
Java Script Interview Question and Answer(31-40).
Javascript Objective Questions And Answers
Javascript Subjective Questions And Answers
Javascript Interview Questions And Answers
MFC Interview Questions And Answers(Subjective)

MFC Interview Questions And Answers(Subjective)

ATL Interview Questions And Answers(Subjective)

COM DCOM Interview Questions And Answers(Subjective)

Win32API Interview Questions And Answers(Subjective)

ActiveX Interview Questions And Answers(Subjective)

R4R, VC++ AllOther Interview Questions And Answers(Subjective)

PL/SQL Interview Questions And Answers(Subjective)

152 PL/SQL Interview Questions And Answers(Subjective)

Asp Objective Questions And Answers
Asp Subjective Questions And Answers
Asp Interview Questions And Answers 1

VB Objective Questions And Answers

Contact US:

Your Name:


Your Email:

Message:

Comments:

Give Your Comments:


Advertiser PRIVACY POLICY ||User PRIVACY POLICY || R4R Group Srvices