Java print/println exam questions
- Categories Java Introduction, Introduction to Java, Java
Here is a quick revision of key items you need to remember before attempting Java print / println questions
- println() prints the string and THEN moves to next line.
- print() prints the string and DOES NOT move to next line.
- \n (newline) Moves the cursor to next line. print (“\n”) is equivalent to println()
- \t (tab) Leaves 4 or 8 bye of space before printing next.
- \”, \\ are examples of how a slash in front of quotes etc. help us to print punctuators which have special meaning in java.
- ASCII code for A – Z = 65 – 90
- ASCII code for a – z = 97 – 122
- To do integer manipulations typecast them to int e.g. int c = (int)’A’;
- To print char equivalent of a number, typecast to char e.g. System.out.print ((char)65);
- To convert to uppercase do minus 32 To convert to lowercase do plus 32
- Do a double check if you are using int or char for all ASCII code questions
- Also attempt print questions which are clubbed with switch and loops
Lets Start!
- What is the output of the following statements?
- System.out.print(“Simply “);
System.out.println(“Coding”);
Choose the correct option:
- Simply Coding
- Simply
Coding
Ans. (i) is the correct output.
Explanation: System.out.print() does not add a new line at the end because of which ‘Coding’ gets printed on the same line as ‘Simply’.
- What is the output of the following?
char c = ‘A’;
short m = 26;
int n = c + m;
System.out.println(n);
Ans: 91.
Explanation: This is because adding ‘char’ with integer data type ‘short’, the lower data type ‘char’ converted to integer ‘short’. i.e. ‘A’ becomes 65, so 65+26 is 91
- Write the output for the following:
System.out.print(“Simply\nt” + “\n” + “\\Coding”);
Ans.
Simply
t
\Coding
- Write the Output:
char ch= ‘F’;
int m=ch;
m=m+5;
System.out.println(m+ ” ” +ch);
Ans. 75 F
Explanation:
ch = ‘F’
int m = ch; — ASCII value of ‘F’ i.e. 70 will be assigned to m
m = m + 5 = 70 + 5 = 75
System.out.println(m+ ” ” +ch); — 75 F
- Give the output and show the dry run too.
public class output1
{
static int m=0,n=0;
public static void main()
{
int m=10, x=20;
{
int n=30;
System.out.println(“m+n=”+(m+n));
}
x+=m+n;
System.out.println(“x=”+x);
}
}
Ans
m+n=40 // (value of ‘n’ is 0 at initialize n=0, but in code block value of ‘n’ is change 0 to 30, so; n=30, m=10=> 30+10 =40)
x=30 // (n=0, m=10 and x=20 => 20+=10+0;)
- State the result of the following statement.
System.out.print(“Simply Coding \n”);
System.out.print(“Online \tTutor \t”);
System.out.print(“and Course.”);
Ans.
- Simply Coding
- Online Tutor and Course.
- State the result of the following statement.
System.out.print(“Welcome \n to \t “);
System.out.println(“Simply Coding “);
System.out.print(“Online Courses”);
Ans.
Welcome
to Simply Coding
Online Courses
- What is the output of following code?
class StringExample{
public static void main(String args[]){
char a= ‘a’;
char b= ‘A’;
System.out.println (“Sum:”+a+b);
System.out.println(“Difference:”+((int)a-(int)b));
}
}
Ans.
Sum:aA
Difference:32
- What will be the output when the following code segment is executed?
System.out.println (“Welcome to \”Simply Coding!\”programming is fun!”);
Ans. Welcome to “Simply Coding!”programming is fun!
- Write the output of the following:
System.out.println(“Simply”+“\n”+“Coding”);
Ans. Simply
Coding