TCS Tech Classes, Iterations and Arrays and String | 2018

1. Each object of the class contains its own copy of instance variables?
Ans: True
2. Which of the below access specifiers can be used with keyword class
Ans: public
3. What is the output of the following program
public class demo{
public static void main(String[]args){
System.out.println(“hi”+args.length);
}
}
Ans: hi 0
4. What is the output of the following program
Class CharRange{
Public static void main(String[]args){
Char ch=127;
Ch++;
println(ch+”=”+(int)ch);
}
}
Ans: Garbage character= -128
5. ------------- is a default package in java
Ans: java.lang
6. The instance of operator returns a _ ______value
Ans: boolean
7. Class Test{ Int x =20;
ANS: 3020
8. Public class Test1{ Int i=10;
ANS: 5010
9. Which of the following statements is true about static methods
ANS: All of the above
10. Which of the following is true?
ANS: A class has no constructor, compiler will provide default constructor
11. Will this code compile?
Class Demo{
Demo(char status) {
System.out.println(“Parameterized constructor”);
}
Public static void main(Strings[]args){
Demo ob = new Demo(‘A’);
} }
ANS: This code will as the default constructor is not used in main()
12. Which methods can access to private attributes of a class ?
ANS: only methods those defined in the same class
13. A constructor
ANS: must have the same name as the class it is declared within
Is used to create objects
May be declared private
A,b and c
14. There can be more than one java class is same file if.(choose all that apply)
ANS: Only 1 class has public access modifier and has the same name as the java.file
15.
Public class Exam1{
Private int a;
Public static void main(Strings[]arg){
/* complete the code using the options so that O is printed in console*/
} }
ANS: System.out.println(new Exam1().a);
16. Public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()).i);
} }
ANS: Compiler error complaining about forward referencing.
17. What if I write static public void instead of public static void while defining main method
ANS: Program compiles and runs successfully (correct answer)
18. Which is not access modifiers ?
ANS: Final
19. What is the output of the below program ?
class Output {
public static void main(Strings args[]){
int x, y=1; x=10; if(x!=10 &&x/0===0)
System.out.println(y);
Else System.out.println(++y);
} }
ANS: 2
20. A method within a class is only accessible by classes that are defined within the same package as the class of the method one of the following is used to enforce such restriction?
ANS: Do not declare the method with any accessibility modifier.
21. What will be the value of a after execution of the following statements;
int a=10,b=14;
a=((a>b)?(b-a):(b+a))
ANS: 24.0
24.
import java.util.*;
public class Employee{
public String firstName;
public String lastName;
public int age;
public char gender;
public Employee(String firstNameForThisObject,String lastNameForThisObject,char gender){ firstName = firstNameForThisObject;
lastName = lastNameForThisObject;
gender=gender;
} public static void main(String[]arg){
Employee employee = new Employee("firstNameForThisObject","lastNameForThisObject",'M'); System.out.print("first name is:"+employee.firstName);
System.out.print("last name is:"+employee.lastName); System.out.print("age is:"+employee.age); System.out.print("gender is:"+employee.gender);
} }
ANS: first name is:firstNameForThisObjectlast name is:lastNameForThisObjectage is:0ge
nder is: (option d)
1. What is stored in the object obj in the following lines of code? Box obj;
Ans: NULL
2. Which is not an Access Modifier?
Ans: Final
3. Which access modifiers in java is accessed by other classes in the same package
or any subclasses of same package or different package?
Ans: Protected
4. Arrange the access levels in order of decreasing accessibility
Ans: Public Default Private
5. What is not true about default constructor. Choose all that apply.
Ans: Any constructor which is always used to create an object can be called
default constructor. It may or may not have parameters.
6. All the user defined classes in java implicitly have a default constructor even if the
constructor is not defined explicitly
Ans: True
7. Which is not Non-Access Modifiers?
Ans: Private
8.______is a default package in Java
Ans: java.lang
9. Which of the following statement is true about static methods
Ans: All of the above (A static method belongs to the class rather than object
of a class, A static method can be invoked without the need for creating an
instance of a class, A static method can access static data member and can
change the value of it)
10. What will be the value of after execution of the following statements:
int a-10, b-14;
a-((a>b)?(b-a):(b+a))
Ans: 24.0
**11. There can be more than one java class in same file if.(Choose all that apply).
Ans: Only 1 class has public access modifier and has the same name as the
.java file
12. What is the output of the following program
public class demo{
public static void main(String[] args){
System.out.println("hi" +args.length);
} }
Ans: hi 0
13. What if I write static public void instead of public static void while defining main
method?
Ans: Program compiles and runs successfully
14. public class AQuestion{
private int i=j;
private int j=10;
public static void main(String args[]){
System.out.println((new Aquestion()),i);
} }
Ans: Compiler error complaining about forward referencing
15. The static can be used for?
Ans: All the above (variable(also known as class variable), method(also known
as class method), nested class)
**16. Can you say that state of an object is similar to attribute of the class?
Ans: True
17. Which keyword can be used to refer current class instance variable?
Ans: this
18._______is the ability to define methods for derived classes
Ans: Polymorphism
19. Constructors can be private
Ans: True
20. How many constructors can be there in a class?
Ans: any number
21. Arrange the access levels in order of decreasing accessibilty
Ans: public default private
22. Which keyword in Java declares a variable whose value cannot change?
Ans: final
23. What is the output of the below program?
class Number
{
public static void main(String[] args)
{
int num=50;
switch(num>=60)
{
case true: System.out.println("Number lesser than 60!");
break;
case false: System.out.println("Number greater than 60!");
break;
default: System.out.println("Default Case");
break;
}
Ans: boolean can not be converted into int
**24. Importing a package imports the subpackages as well?
Ans: false
25. A constructor
Ans: A, B and C ( a) must have the same name as the class it is declared, b) is
used to create objects, c) may be declared private)
**26. public class Exam1
{ public static void main(String args[])
{ private int a=0;
System.out.println(a++);
}}
What is the output of the above program?
Ans: Compile time error(private & static can’t be in local)
27.Which methods can access to private attributes of a class?
Ans: Only methods those defined in the same class
29. What is the output of the below mentioned code
public class Sample
{
public static void main(String[] args)
{
Demo d=new Demo(20);
System.out.println(d,num);
}
class Demo
{
final int num;
public Demo(int n)
{
num=n;
} }
Ans: 20
30. public class Test1{
static{
System.out.println("Static block");
}
public static void main(String args[]){
System.out.println("Main method");
}
public static void staticMethod(){
System.out.println("Static method");
} }
What will be the output?
Ans: Static block Main method
31. What is the output of the following program?
class MyClass
{
public static void main(String[] args)
{
int x;
System.out.println("Begin Main");
x=test();
System.out.println("End Main");
}
static void test()
{
System.out.println("Inside Test()");
return 0;
} }
Ans: Compile time error (unexpected return value)
32. What if I do not provide the String array as the argument to the main method?
Ans: Run-time error
33. Which of the below access specifiers can be used with keyword class?
Ans: public
34. What will the below mentioned program print?
public class Sample
{
public static void main(String args[])
{
boolean b=false;
if(b=true)
System.out.println("True");
else
System.out.println("False");
} }
Ans: True 1. A/An _ _ _ _ variable is shared by all instances of the class. It exists even before an object is created.
Ans: static
public class Test{
private String myName;
private String lastName;
public Test(String myName, String lastName){
System.out.println("Hey, I am parameterized, so I am called!");
}
public Test(){
System.out.println("Hey,I am default, so any how I am always called!");
}
public static void main(String[]arg){
Test test = new Test("soham","batavia");
}
}
ANS: Hey I am parameterized, so I am called!
Which four options describe the correct default values for the types indicated? 1. Int=0,
2.String=null, 3.Dog=null, 4.boolean=false, 5.float=0.0, 6.boolean=true
ANS: 1,2,3,4,5
class IfExample {
public static void main(String[]args){
int var1;
if(var1){
System.out.println("Inside If Condition");
}
}
}
Ans: compile time error(local variables must be initialize)
class DummyClass {
static int code = 1;
}
public class DummyTester {
public static void main(String a[]){
DummyClass dummyObj1 = new DummyClass();
DummyClass dummyObj2 = new DummyClass();
System.out.println(dummyObj1.code);
dummyObj2.code++;
dummyObj1.code=dummyObj1.code+1;
System.out.println(dummyObj2.code);
}
}
ANS: 1 3
public class Sample {
public static void main(String []a){
Employee e1 = new Employee(1,"A");
e1.count++;
Employee e2 = new Employee(1,"A");
e2.count++;
System.out.println(Employee.count);
}
}
class Employee{
int id;
String name;
static int count = 0;
Employee(int i,String s){
id=i;
name=s;
}
}
ANS: 2
What are not the characteristics of object ?
Ans: variables
package p1;
public class Sample{
public static void main(String[]args){
Employee emp = new Employee(1,"A");
System.out.println(emp.id);
}
}
package p2;
class Employee
{
int id;
String name;
Employee(int i,String s){
id=i;
name=s;
}
}
ANS: compile time error
class Exam1{
public static void main(String []args){
int a=5;
if(a<5)
System.out.println("Less than 5");
else
System.out.println("Greater than 5");
else if(a==5)
System.out.println("Equals");
}
}
ANS: compiler time error
public class Test{
private String myName;
private String lastName;
public Test(){
System.out.println("Hey, I am default, so any how I am always called!");
}
public static void main(String[]a){
Test test = new Test();
Test anotherTest = new Test();
}
}
ANS: Hey, I am default, so any how I am always called!
Hey, I am default, so any how I am always called!
public class Test{
public static void main(String[]a){
int i=5;
switch(i%2){
case 0: System.out.println("Number is even");
case 1: System.out.println("Number is odd");
default:System.out.println("Can not decide");
}
}
}
ANS: Number is odd can not decide
You read the following statement in a java program that compiles and executes.
Submarine.dive(deoth);
ANS: dive must be a method
Public class Test{
Private static int noOfObjects;
Public static void main(String[]args){
Private static int noOfInstances=10;
Systm.out.print(“noOfObjects:”+noOfObjects);
Systm.out.print(“noOfInstances:”+noOfInstances);
} }
Ans: compilation error(static keyword can not be applied inside method)
Which of the following statements is TRUE with respect to class and members of a class?
ANS: it is mandatory to specify return type along with the method definition in a class
public class Sample
{
public static void main(String[] args)
{
Demo d=new Demo(20);
System.out.println(d.num);
} }
class Demo
{
final int num;
public Demo(int n)
{
num=n;
} }
ANS: compile time error =final value can not be changed
public class SwitchControl
{
public static void main(String[] args){
int i=10;
switch(i);
break;
}
}
ANS: Compile time Error
Which of the following is not true ?
ANS: the protected modifier is more restrictive than default
What is the return type of a Constructor ?
Ans: No return type
class Test{
public static void main(String args[]){
MyBlock:
{
int i=10;
}
i=20;
System.out.println(i);
} }
ANS: compile time error can not find symbol i
Which access modifiers in java is accessed by other classes in same package or any subclasses of
same package or different package?
Ans: protected
class Test1{
int i=10;
public static void main(String args[]){
int i=50;
System.out.println(i);
Test1 obj=new Test1();
obj.printNumber(obj.i);
}
public void printNumber(int i){
System.out.println(i);
} }
ANS: 50 10
Can you say that behaviour of an object is similar to method of the class?
ANS: true
class MyMethod{
static int func1(){
System.out.println("Inside Func1");
return 10;
}
static int func2(){
System.out.println("Inside Func2");
return func1();
}
static int func3(){
System.out.println("Inside Func3");
return func1()+func2();
}
public static void main(String[]a){
System.out.println(func3());
}
}
Ans: inside Func3 inside Func1 inside Func2 inside Func1 20
class StatusDemo{
int id;
char status;
StatusDemo(){
id++;
}
StatusDemo(char status){
System.out.println("Id:="+id+"Status:"+status);
}
public static void main(String args[]){
StatusDemo ob= new StatusDemo('D');
}
}
ANS: Id:=0 Status:D
class MyClass{
int i;
float j;
public static void main(String args[]){
System.out.println("i="+i+"j="+j);
}
}
ANS: compile error instance accesss by obi
Can we declare an abstract static method?
ANS: false

No comments:

Post a Comment