Java Question:
Q1: What will be the output of the following code?
public class MyClass {
private int myInt;
public MyClass(int myInt) {
this.myInt = myInt;
}
public int getMyInt() {
return myInt;
}
public void setMyInt(int myInt) {
this.myInt = myInt;
}
}
public class Main {
public static void main(String[] args) {
MyClass obj1 = new MyClass(10);
MyClass obj2 = obj1;
obj1.setMyInt(20);
System.out.println(obj2.getMyInt());
}
}
a) 10
b) 20
c) Compilation error
d) Runtime error
Answer: b) 20
Explanation:
Two objects of MyClass are created: obj1 and obj2
obj1 is initialized with a value of 10 using the constructor
obj2 is assigned a reference to obj1 (both now point to the same object)
obj1's myInt value is changed to 20 using the setMyInt() method
obj2's getMyInt() method returns the value 20 because obj2 points to the same object as obj1
The output to the console is 20
****************************************************************************************************
Typescript Question
Q2. What will be the output of the following code?
a) true true
b) true false
c) false true
d) false false
Answer: b) true false
Explanation:
Two String objects are created: s1 and s2
s1 is initialized with a string literal "hello"
s2 is initialized with a new String object that also contains the string "hello"
The == operator compares object references, so s1 == s2 will return false because they refer to
different objects
The equals() method compares the contents of the objects, so s1.equals(s2) will return true
because they contain the same string
The output to the console is:
false (for s1 == s2)
true (for s1.equals(s2))
Q3. What will be the output of the following code?
a) Hello
b) World
c) HelloWorld
d) Compile time error
Answer: c) HelloWorld
Explanation:
Two strings, str1 and str2, are initialized with the values "Hello" and "World", respectively
The concat() method is used to concatenate str1 and str2, and the result is assigned to a new
string variable named str3
The concat() method returns a new string that represents the concatenation of the calling string
(in this case, str1) with the string that is passed as an argument (in this case, str2).
The final value of str3 is "HelloWorld", which is printed to the console.
*******************************************************************************************************
Csharp Question
Q4. What will be the output of the following code?
a) 6
b) 12
c) 18
d) 30
Answer: c) 18
Explanation:
An array of integers named myArray is initialized with 5 elements
A variable sum is initialized with a value of 0
A for loop iterates through each element of myArray
The if statement checks if the current element is greater than 5. If it is, the current element is
added to sum. If it isn't, the loop skips to the next iteration using the continue keyword.
In this case, the if statement is true for the elements 6, 8, and 10, and the corresponding values
are added to sum.
The final value of sum is 18, which is printed to the console.
Q5. What will be the output of the following code?
a) true, true, true, true
b) true, true, false, true
c) false, false, true, true
d) false, true, false, true
Answer: b) true, true, false, true
Explanation:
A String s1 is initialized with the value "hello"
Another String s2 is initialized with the value "hello" using the new keyword to create a new
object
A third String s3 is initialized with the value "hel" + "lo", which is a compile-time constant
expression and is evaluated to "hello"
The first comparison s1 == s2 checks if s1 and s2 are the same object, which is false because
s2 is created with the new keyword and therefore points to a different object in memory
The second comparison s1 == s3 checks if s1 and s3 are the same object, which is true
because the compiler optimizes the constant expression and points both s1 and s3 to the same
object in memory
The third comparison s1.equals(s2) checks if s1 and s2 have the same value, which is true
because String objects have a equals() method that compares their values and not their
memory locations
The fourth comparison s1.equals(s3) checks if s1 and s3 have the same value, which is true
because their values are both "hello"