CanGo Networks – Interview Questions with Answers

In this post, we will discuss about CanGo Networks – Interview Questions with Answers.
1) What is the output for the below code snippet?
public class CanGo {
int a = 1111;
static {
a—–a;
}
static {
a+++++a;
}
public static void main(String[] args) {
System.out.println(a);
}
}
Output: Will throw Compilation error saying ‘Cannot make a static reference to the non-static field a.
2) What is the output for the below code snippet?
int arr[] = {0,1,2,3,4,5,6,7,8,9};
int n=6;
n = arr[n/2];
System.out.println(arr[n/2]);

Output: 1

 

3) Find output for the below snippet.
public class CanGo {
int x=1;
static int y;
{
x++;
}
//++x;
{
y = ++x;
}
public static void main(String[] args) {
System.out.println(y);
}
}
Output: 0.
Reason: Since, y is static int value, it will be initialized with 0 and not gets incremented or decremented.

 

4) Find the output for the below snippet
public class CanGo {
int a = 08;
static int b = 07;
static int c =1234;
public static void main(String[] args) {
System.out.println(” “+b+c);
}
}
Output: Compilation error saying – The literal 08 of type int is out of range.
Reason: Please remember – In Java – Any number prefixed with a 0 is considered as octal value. Octal numbers can only use digits 0-7, similar to decimal can use only 0-9, and binary can use only 0-1.

 

5) What happens for a null object?
public static void main(String[] args) {
CanGo cg=null;
cg.show();
}
static void show() {
// TODO Auto-generated method stub
System.out.println(“name”);
}
Output: name
Reason: Here object (cg) is set to null. Now the Garbace Collector (GC) will check whether that object is assigned to other reference or not. If the object is not assigned to any reference then, that object will be removed from heap by Garbage Collector. But, in this case, the object is set to null first and immediately in the next line, it is being referred for calling show() method.

 

6)  String Concatenation Program
String s1=”abc”;
String s2 = s1;
s1+=”d”;
System.out.println(s1 + ” “+ s2 + ” “+ (s1==s2));
System.out.println(s1+ ” “+ s2 + ” “+ (s1==s2));

Output: abcd abc false

 

7) True or False
int i1=128;
int i2=128;
System.out.println(i1==i2);
int i3 = 127;
int i4=127;
System.out.println(i3==i4);
Output: true true

 

8) Comparing Operator Program
int x=4;
int y=1;
if(x=y)
{
System.out.println(“not equal”);
}
else{
System.out.println(“equal”);
}
Output: Compilation Error – Type mismatch: cannot convert from int to boolean
Reason: = operator is assignment operator, not comparing operator.

 

9) String Comparison Code Snippet
static String s1=”abc”;
static String s2=”abc”;
static String s3=”xyz”;
static String s4=”xyz”;
public static void main(String[] args) {
System.out.println(“Compared: “+ (s1==s2));
System.out.println(“equals: “+ (s1==s4));
System.out.println(“equals: “+ (s1.equals(s4)));
System.out.println(“Compared: “+ (s3.equals(s4)));

}
Output:
Compared: true
equals: false
equals: false
Compared: true
Reason: String objects cannot be compared with == operator. They can be compared only with equals method.

 

10) Code Snippet on Arrays
public class CanGo {
int arr[] = new int[5];
public static void main(String[] args) {
CanGo cg = new CanGo();
int a = cg.arr[4];
System.out.println(a);
}
}
Output: 0
Reason: A variable declared with class level scope will be assigned with its initial value. Eg. If it is int, 0 is the initial value.

 

11) Inheritance Program Snippet
public class CanGo {
public void method()
{
}
}
public class CanGoChild extends CanGo {
public void method()
{
}
}
Output: No errors will be thrown. Method Overriding is applied here.

 

12) Are there errors present in the below program?
public class CangoChild extends CanGo{
public void method()
{

}
public void act(int i)
{

}
}

public class CanGo {
public void method()
{

}
public void act(Integer i)
{

}
}
Output: No error.

 

13) Arithmetic Operations 
public class CanGoNetworks {
static int a =1234;
public void go(){
int a = 0;
go(a);
}
private void go(int a2) {
// TODO Auto-generated method stub
System.out.println(a2);

}
public static void main(String[] args) {
CanGoNetworks cg = new CanGoNetworks();
cg.go();

}
}
Output: 0.
Note: If we do not initialize int a value inside go() method, it will throw compilation error saying, a should be initialized.

 

14) Get the output for the below program
public class CanGo {
static int i=11;
static int method1(int i)
{
return method2(i*=11);
}

private static int method2(int i2) {
return method3(i2/=11);
}

private static int method3(int i3) {
return method4(i3-=11);
}

private static int method4(int i4) {
return method5(i4+=11);
}

private static int method5(int i5) {
return i5;
}

public static void main(String[] args) {
System.out.println(method1(i));
}
}

Output: 11

 

16) Please work on deriving the below pattern
1   5
2  4
3
2  4
1   5