Multiple choice questions for Java io,files and inheritance
1. abishekvk@outlook.com
1. Which of these is correct way of inheriting class A by class B?
a) class B + class A {}
b) class B inherits class A {}
c) class B extends A{}
d) class B extends class A {}
2. Which of the following statements are incorrect?
a) public members of class can be accessed by any code in the program.
b) private members of class can only be accessed by other members of the class.
c) private members of class can be inherited by a sub class, and become protected
members in sub class.
d) protected members of a class can be inherited by a sub class, and become private
members of the sub class.
3. What is the output of this program?
class A {
int i;
void display() {
System.out.println(i);
}
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class inheritance_demo {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 0
b) 1
c) 2
d) Compilation Error
4. What is the output of this program?
2. abishekvk@outlook.com
class A {
int i;
}
class B extends A {
int j;
void display() {
super.i = j + 1;
System.out.println(j + " " + i);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) 2 3
d) 3 2
5. What is the output of this program?
class A {
public int i;
private int j;
}
class B extends A {
void display() {
super.j = super.i + 1;
System.out.println(super.i + " " + super.j);
}
}
class inheritance {
public static void main(String args[])
{
B obj = new B();
3. abishekvk@outlook.com
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 2 2
b) 3 3
c) Runtime Error
d) Compilation Error
6. What is the output of this program?
class A {
public int i;
public int j;
A() {
i = 1;
j = 2;
}
}
class B extends A {
int a;
B() {
super();
}
}
class super_use {
public static void main(String args[])
{
B obj = new B();
System.out.println(obj.i + " " + obj.j)
}
}
a) 1 2
b) 2 1
c) Runtime Error
d) Compilation Error
7. What is the output of this program?
class A {
4. abishekvk@outlook.com
public int i;
protected int j;
}
class B extends A {
int j;
void display() {
super.j = 3;
System.out.println(i + " " + j);
}
}
class Output {
public static void main(String args[])
{
B obj = new B();
obj.i=1;
obj.j=2;
obj.display();
}
}
a) 1 2
b) 2 1
c) 1 3
d) 3 1
8. A class member declared protected becomes member of subclass of which type?
a) public member
b) private member
c) protected member
d) static member
9. Which of the following is true about inheritance in Java?
1) Private methodsare final.
2) Protectedmembersare accessiblewithinapackage and
inheritedclassesoutside the package.
3) Protectedmethodsare final.
4) We cannot override privatemethods.
a) 1,2 and 4
b) Only 1 and 2
c) 1,2 and 3
d) 2,3 and 4
5. abishekvk@outlook.com
10. What will be the output of this program?
class A
{
int b = 50;
}
class B extends A
{
int b = 20;
}
public class MainClass
{
public static void main(String[] args)
{
A a = new B();
System.out.println(a.b);
}
}
a) Compiler error
b) 20
c) 50
d) None of the above
11. What is the output of this program?
import java.io.*;
class filesinputoutput {
public static void main(String args[]) {
InputStream obj = new FileInputStream("inputoutput.java");
System.out.print(obj.available());
}
}
Note: inputoutput.java is stored in the disk.
a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
12. Which of these class can be used to implement input stream that uses a character array
as the source?
a) BufferedReader
b) FileReader
c) CharArrayReader
d) FileArrayReader
13. What is the output of this program?
import java.io.*;
class Chararrayinput {
6. abishekvk@outlook.com
public static void main(String[] args) {
String obj = "abcdef";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0,length,c,0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 0, 3);
int i;
try {
while ((i = input1.read()) != -1) {
System.out.print((char)i);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
a) Abc
b) Abcd
c) Abcde
d) Abcdef
14. What is the output of this program?
import java.io.*;
class Chararrayinput {
public static void main(String[] args) {
String obj = "abcdefgh";
int length = obj.length();
char c[] = new char[length];
obj.getChars(0, length, c, 0);
CharArrayReader input1 = new CharArrayReader(c);
CharArrayReader input2 = new CharArrayReader(c, 1, 4);
int i;
int j;
try {
while ((i = input1.read()) == (j = input2.read())) {
System.out.print((char)i);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
7. abishekvk@outlook.com
}
}
a) Abc
b) Abcd
c) Abcde
d) None of the mentioned
15. Which of these classes can return more than one character to be returned to input
stream?
a) BufferedReader
b) Bufferedwriter
c) PushbachReader
d) CharArrayReader
16. Which of these class contains the methods used to write in a file?
a) FileStream
b) FileInputStream
c) BUfferedOutputStream
d) FileBufferStream
17. Which of these exception is thrown in cases when the file specified for writing it not
found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
18. Which of these methods are used to read in from file?
a) get()
b) read()
c) scan()
d) readFileInput()
19. Which of these values is returned by read() method is end of file (EOF) is encountered?
a) 0
b) 1
c) -1
d) Null
20. Which of these methods is used to write() into a file?
a) put()
b) putFile()
c) write()
d) writeFile()