Technical Programming Output Questions
1.
// filename Main.java
class Test {
protected int x, y;
}
class Main {
public static void main(String args[]) {
Test
t = new Test();
System.out.println(t.x
+ " " + t.y);
}
}
2.
// filename Test.java
class Test {
public static void main(String[] args) {
for(int i = 0; 1; i++) {
System.out.println("Hello");
break;
}
}
}
3.
class Main {
public static void main(String args[]) {
System.out.println(fun());
}
int fun() {
return 20;
}
}
4.
class Test {
public static void main(String args[]) {
System.out.println(fun());
}
static int fun() {
static int x= 0;
return ++x;
}
}
5.
package main;
class Base {
public void Print()
{
System.out.println("Base");
}
}
class Derived extends Base {
public void Print()
{
System.out.println("Derived");
}
}
class Main {
public static void DoPrint(Base o)
{
o.Print();
}
public static void main(String[] args)
{
Base
x = new Base();
Base
y = new Derived();
Derived
z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}
6.
package main;
// filename Main.java
class Point {
protected int x, y;
public Point(int _x, int _y)
{
x
= _x;
y
= _y;
}
}
public class Main {
public static void main(String args[])
{
Point
p = new Point();
System.out.println("x
= " +
p.x + ", y = " + p.y);
}
}
7.
class Test {
int y = 2;
int x = y+2;
public static void main(String[] args) {
Test
m = new Test();
System.out.println("x
= " +
m.x + ", y = " + m.y);
}
}
8.
public class Test
{
int x = 2;
Test(int i) { x = i; }
public static void main(String[] args) {
Test
t = new Test(5);
System.out.println("x
= " +
t.x);
}
}
9.
public class Calculator
{
int num = 100;
public void calc(int num) { this.num = num * 10; }
public void printNum() {
System.out.println(num); }
public static void main(String[] args)
{
Calculator
obj = new Calculator();
obj.calc(2);
obj.printNum();
}
}
10.
public class MyStuff
{
String
name;
MyStuff(String
n) { name = n; }
public static void main(String[] args)
{
MyStuff
m1 = new MyStuff("guitar");
MyStuff
m2 = new MyStuff("tv");
System.out.println(m2.equals(m1));
}
@Override
public boolean equals(Object obj)
{
MyStuff
m = (MyStuff) obj;
if (m.name != null) { return true; }
return false;
}
}
11.
public class Test
{
public static void main(String[] args)
{
StringBuilder
s1 = new StringBuilder("Java");
String
s2 = "Love";
s1.append(s2);
s1.substring(4);
int foundAt = s1.indexOf(s2);
System.out.println(foundAt);
}
}
12.
class Writer
{
public
static void write()
{
System.out.println("Writing...");
}
}
class Author extends Writer
{
public
static void write()
{
System.out.println("Writing
book");
}
}
public class Programmer extends Author
{
public
static void write()
{
System.out.println("Writing
code");
}
public
static void main(String[] args)
{
Author
a = new Programmer();
a.write();
}
}
13.
class First
{
public
First() { System.out.println("a"); }
}
class Second extends First
{
public
Second() { System.out.println("b"); }
}
class Third extends Second
{
public
Third() { System.out.println("c"); }
}
public class MainClass
{
public
static void main(String[] args)
{
Third
c = new Third();
}
}
14.
class First
{
int i =
10;
public
First(int j)
{
System.out.println(i);
this.i
= j * 10;
}
}
class Second extends First
{
public
Second(int j)
{
super(j);
System.out.println(i);
this.i
= j * 20;
}
}
public class MainClass
{
public
static void main(String[] args)
{
Second
n = new Second(20);
System.out.println(n.i);
}
}
15.
import java.util.*;
class I
{
public
static void main (String[] args)
{
Object
i = new ArrayList().iterator();
System.out.print((i
instanceof List) + ", ");
System.out.print((i
instanceof Iterator) + ", ");
System.out.print(i
instanceof ListIterator);
}
}
16.
class ThreadEx extends Thread
{
public
void run()
{
System.out.print("Hello...");
}
public
static void main(String args[])
{
ThreadEx
T1 = new ThreadEx();
T1.start();
T1.stop();
T1.start();
}
}
17.
public class Test
{
public
int getData() //getdata() 1
{
return
0;
}
public
long getData() //getdata 2
{
return
1;
}
public
static void main(String[] args)
{
Test
obj = new Test();
System.out.println(obj.getData());
}
}
18.
public class Test
{
public
int getData(String temp) throws IOException
{
return
0;
}
public
int getData(String temp) throws Exception
{
return
1;
}
public
static void main(String[] args)
{
Test
obj = new Test();
System.out.println(obj.getData("GFG"));
}
}
19.
class Derived
{
protected
final void getDetails()
{
System.out.println("Derived
class");
}
}
public class Test extends Derived
{
protected
final void getDetails()
{
System.out.println("Test
class");
}
public
static void main(String[] args)
{
Derived
obj = new Derived();
obj.getDetails();
}
}
20.
class Derived
{
public
void getDetails(String temp)
{
System.out.println("Derived
class " + temp);
}
}
public class Test extends Derived
{
public
int getDetails(String temp)
{
System.out.println("Test
class " + temp);
return
0;
}
public
static void main(String[] args)
{
Test
obj = new Test();
obj.getDetails("GFG");
}
}
21.
Which methods are provided by the PrintStream
class?
a. Read data to another stream
b. Write
data to another stream
c. Read
data to same stream
d. Write
data to same stream
22.
Daemon thread provides services to user threads
for background supporting tasks,It has no role in life than to serve user
threads.
a. True
b. False
23.
class Helper
{
private
int data;
private
Helper()
{
data
= 5;
}
}
public class Test
{
public
static void main(String[] args)
{
Helper
help = new Helper();
System.out.println(help.data);
}
}
24.
public class Test implements Runnable
{
public
void run()
{
System.out.printf("
Thread's running ");
}
try
{
public
Test()
{
Thread.sleep(5000);
}
}
catch
(InterruptedException e)
{
e.printStackTrace();
}
public
static void main(String[] args)
{
Test
obj = new Test();
Thread
thread = new Thread(obj);
thread.start();
System.out.printf("
GFG ");
}
}
25.
public class Test
{
public
Test()
{
System.out.printf("1");
new
Test(10);
System.out.printf("5");
}
public
Test(int temp)
{
System.out.printf("2");
new
Test(10, 20);
System.out.printf("4");
}
public
Test(int data, int temp)
{
System.out.printf("3");
}
public
static void main(String[] args)
{
Test
obj = new Test();
}
}
26.
class demo1 {
public
static void main(String args[])
{
String
str1 = "java";
char
arr[] = { 'j', 'a', 'v', 'a', ' ', 'p',
'r',
'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' };
String
str2 = new String(arr);
System.out.println(str1);
System.out.println(str2);
}
}
27.
class demo2 {
public
static void main(String args[])
{
String
str1 = "";
char
arr[] = { 'j', 'a', 'v', 'a', ' ', 'p',
'r',
'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' };
String
str2 = new String(arr);
String
str3 = new String(str2);
System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
}
}
28.
class demo3 {
public
static void main(String args[])
{
byte[]
arr = { 97, 98, 99, 100, 101 };
String
str2 = new String(arr);
System.out.println(str2);
}
}
29.
class demo4 {
public
static void main(String args[])
{
String
str = "Java Programming";
char
ch = str.charAt(2);
System.out.println(ch);
}
}
30.
class demo5 {
public
static void main(String args[])
{
String
str = "Java Programming";
char
arr[] = new char[10];
str.getChars(0,
4, arr, 0);
System.out.println(arr);
}
}
31.
class demo7 {
public
static void main(String args[])
{
String
str = "Java Programming";
String
str1 = "Java Programming";
String
str2 = str1;
if
(str.equals(str1))
System.out.println("Equal
Case 1");
if
(str == str1)
System.out.println("Equal
Case 2");
}
}
32.
class demo8 {
public
static void main(String args[])
{
String
str = "Java Programming";
String
str1 = "Programminggggg";
if
(str.regionMatches(5, str1, 0, 11))
System.out.println("Same");
}
}
33.
class demo9 {
public
static void main(String args[])
{
String
str = "Java Programming";
String
str1 = "Java";
if
(str.startsWith("J"))
System.out.println("Start
Same");
if
(str.endsWith("T"))
System.out.println("End
Same");
}
}
34.
class demo10 {
public
static void main(String args[])
{
String
str = "JavaProgramming";
String
str1 = "Java";
System.out.println(str.compareTo(str1));
System.out.println(str1.compareTo(str));
}
}
35.
class demo12 {
public
static void main(String args[])
{
String
str = "Java Programming";
String
str1 = "Java";
String
str2 = str.substring(5);
System.out.println(str2);
System.out.println(str.substring(5,
9));
}
}
36.
import java.util.*;
public class Treeset {
public
static void main(String[] args)
{
TreeSet<String>
treeSet = new TreeSet<>();
treeSet.add("Free");
treeSet.add("For");
treeSet.add("Java Training");
treeSet.add("Free For Java Training");
for
(String temp : treeSet)
System.out.printf(temp
+ " ");
System.out.println("\n");
}
}
37.
public class MyFirst {
public static void main(String[] args) {
MyFirst obj = new MyFirst(n);
}
static int a = 10;
static int n;
int b = 5;
int c;
public MyFirst(int m) {
System.out.println(a + ", " + b + ", " + c + ", " + n + ", " + m);
}
// Instance Block
{
b = 30;
n = 20;
}
// Static Block
static
{
a = 60;
}
}
38.
public class testincr
{
public static void main(String args[])
{
int i = 0;
i = i++ + i;
System.out.println(“I = ” +i);
}
}
39.
public class testmeth
{
static int i = 1;
public static void main(String args[])
{
System.out.println(i+” , “);
m(i);
System.out.println(i);
}
public void m(int i)
{
i += 2;
}
}
40.
Consider the two methods (within the same
class)
public static int foo(int a, String s)
{
s = “Yellow”;
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = “Blue”;
a = foo(a,s);
System.out.println(“a=”+a+” s=”+s);
}
public static void main(String args[])
{
bar();
}
41)Among these
expressions, which is(are) of type String?
(a) “0” (b) “ab” +
“cd”
(c) ‘0’
(d) Both (A) and (B) above (e) (A), (B) and (C) above.
42) Consider the
following code fragment
Rectangle r4 = new Rectangle();
r4.setColor(Color.blue);
Rectangle r2 = r4;
r2.setColor(Color.red);
After the above piece
of code is executed, what are the colors of r4 and
r2 (in this order)?
(a) Color.blue
Color.red
(b) Color.blue
Color.blue
(c) Color.red
Color.red
(d) Color.red
Color.blue
(e) None of the above.
43) What is the type
and value of the following expression? (Notice the integer division)
-4 + 4/2 + 2*-3 + 5.0
(a) int -5 (b) double
-4.5
(c) int -4
(d) double -5.0 (e) None of the above.
44) What is printed by
the following statement?
System.out.print(“Hello,\nworld!”);
(a) Hello, \nworld!
(b) Hello, world!
(c)
(d) “Hello, \nworld!” (e) None of the above.
45) Consider the two
methods (within the same class)
public static int foo(int a, String s)
{
s = “Yellow”;
a=a+2;
return a;
}
public static void bar()
{
int a=3;
String s = “Blue”;
a = foo(a,s);
System.out.println(“a=”+a+” s=”+s);
}
public static void main(String args[])
{
bar();
}
What is printed on
execution of these methods?
(a) a = 3 s = Blue (b)
a = 5 s = Yellow (c) a = 3 s = Yellow
(d) a = 5 s = Blue (e) none of the above.
46) Which of the
following variable declaration would NOT compile in a java program?
(a) int var; (b) int
VAR; (c) int var4; (d) int var_4; (e) int 4_var;.
47) Consider the
following class definition:
public class MyClass
{
private int value;
public void setValue(int i){ / code / }
// Other methods…
}
The method setValue
assigns the value of i to the instance field value. What could you write for
the implementation of setValue?
(a) value = i; (b)
this.value = i; (c) value == i;
(d) Both (A) and (B) and above (e) (A), (B) and (C) above.
48) Which of the
following is TRUE?
(a) In java, an
instance field declared public generates a compilation error.
(b) int is the name of a class available in the package java.lang
(c) Instance variable names may only contain letters and digits.
(d) A class has always a constructor (possibly automatically supplied by the
java compiler).
(e) The more comments in a program, the faster the program runs.
49) A constructor
(a) Must have the same
name as the class it is declared within.
(b) Is used to create objects.
(c) May be declared private
(d) Both (A) and (B) above
(e) (a), (b) and (c) above.
50) Consider,
public class MyClass
{
public MyClass(){/code/}
// more code…
}
To instantiate
MyClass, you would write?
(a) MyClass mc = new
MyClass();
(b) MyClass mc = MyClass();
(c) MyClass mc = MyClass;
(d) MyClass mc = new MyClass;
(e) The constructor of MyClass should be defined as, public void
MyClass(){/code/}.
1.00
2.compile error
3.compile error
4.compile error
5.Base dervied dervied
6.compile error
7.x=4 y=2
8.x=5
9.20
10.The output is true and MyStuff does NOT fulfill the Object.equals() contract
11.4
12.writing book
13.a b c
14.10
200
400
15.false, true, false
16.Run Time Exception
17.compile error
18.compile error
19.compile error
20.compile error
21.b
22.a
23.compile error
24.compile error
25.12345
26.java
java programming
27.java programming java programming
28.abcde
29.v
30.java
31.Equal Case 1
Equal Case 2
32.same
33.start same
34.11 -11
35.Programming
Prog
36.Free For Javatraining
37.60, 30, 0, 20, 0
38.i=2
39.1 1
40.a=5 s=BLUE
41.A) d
42.A) c
43.A) d
44.A) c
45.A) d
46.A) e
47.A) d
48)A) d
49)A) e
50)A) a
No comments:
Post a Comment