Fiveable
Fiveable

System.out.println

Definition

System.out.println is a Java statement used to display output on the console. It prints the specified message or value and adds a new line character at the end.

Analogy

Think of System.out.println as a text message you send to your friend. You type out your message (the output) and hit send, and your friend receives it on their phone screen (the console).

Related terms

Variable: A variable is a named storage location in computer memory that holds a value. It can be used to store data that can be accessed and manipulated in a program.

Method: A method is a block of code that performs a specific task. It can take input parameters, perform operations, and return a result.

String: In Java, String refers to a sequence of characters. It is used to represent textual data and is one of the most commonly used data types in programming.

"System.out.println" appears in:

Practice Questions (20+)

  • What would be the result of executing the following code? System.out.println(alice.age);
  • What would be the result of executing the following code? System.out.println(Student.gpa);
  • What will the following code print out? public class NamePrinter { public void printName(String name) { System.out.println("Your name is: " + name); } public void printAge(int age) { System.out.println("Your age is: " + age); } public static void main(String[] args) { NamePrinter np = new NamePrinter(); np.printName("John"); np.printAge(32); } }
  • What will the following code output? public class Book { public String title; public String author; public int pages; public Book(String bookTitle, String bookAuthor, int bookPages) { title = bookTitle; author = bookAuthor; pages = bookPages; } public void printBookInfo() { System.out.println("Title: " + title); System.out.println("Author: " + author); System.out.println("Pages: " + pages); } public static void main(String[] args) { Book book = new Book("The Great Gatsby", "F. Scott Fitzgerald", 180); book.printBookInfo(); } }
  • What is the output of the following code? public class Employee { public String name; public int salary; public Employee(String empName, int empSalary) { name = empName; salary = empSalary; } public void printEmployeeInfo() { System.out.println("Name: " + name); System.out.println("Salary: $" + salary); } public void giveRaise(int raiseAmount) { salary += raiseAmount; } public static void main(String[] args) { Employee emp = new Employee("John", 50000); emp.printEmployeeInfo(); emp.giveRaise(2000); emp.printEmployeeInfo(); } }
  • Consider the following class definition: public class Student { private String name; private int age; private String major; public Student(String studentName, int studentAge, String studentMajor) { name = studentName; age = studentAge; major = studentMajor; } public String getName() { return name; } public int getAge() { return age; } public String getMajor() { return major; } } Assume that the following code segment appears in a class other than Student: Student student = new Student("Alice", 19, "Computer Science"); String name = student.getName(); int age = student.getAge(); String major = student.getMajor(); System.out.println(name + " is " + age + " years old and majoring in " + major + "."); What is printed as a result of executing the code segment?
  • Consider the following method: public static int calculateSum(int n) { if (n == 1) { return 1; } else { return n + calculateSum(n - 1); } } Assume that the following code segment appears in a class: int sum = calculateSum(5); System.out.println("Sum of numbers from 1 to 5: " + sum); What is printed as a result of executing the code segment?
  • Consider the following method: public static boolean isPalindrome(String word) { int length = word.length(); for (int i = 0; i < length / 2; i++) { if (word.charAt(i) != word.charAt(length - 1 - i)) { return false; } } return true; } Assume that the following code segment appears in a class: boolean result = isPalindrome("racecar"); System.out.println("Is 'racecar' a palindrome? " + result); What is printed as a result of executing the code segment?
  • Consider the following method: public static boolean isEven(int number) { return number % 2 == 0; } Assume that the following code segment appears in a class: boolean result = isEven(7); System.out.println("Is 7 an even number? " + result); What is printed as a result of executing the code segment?
  • Consider the following method: public static String capitalize(String word) { return word.substring(0, 1).toUpperCase() + word.substring(1); } Assume that the following code segment appears in a class: String result = capitalize("hello"); System.out.println("Capitalized word: " + result); What is printed as a result of executing the code segment?
  • What is the result of the following code?<br>Integer x = new Integer(10);<br>int y = x;<br>System.out.println(y);
  • What is the result of the following code?<br>Integer x = new Integer(22);<br>int y = x.intValue();<br>System.out.println(y);
  • What is the result of the following code?<br>Integer x = new Integer(42);<br>int y = (int) x;<br>System.out.println(y);
  • What is the result of the following code?<br>Double d = 3.14;<br>System.out.println(d);
  • What is the result of the following code?<br>int x = 5;<br>Integer y = x;<br>System.out.println(y);
  • What is the result of the following code?<br>String str = "123";<br>Integer x = Integer.parseInt(str);<br>System.out.println(x + 1);
  • Suppose there is a Car object whose constructor takes the brand (String) and year (int) parameters. If we create two new objects "Car c1 = new Car("Toyota", 2005);" and "Car c2 = new Car("Toyota", 2005);" what will be the output of "System.out.println(c1.equals(c2));"?
  • Given the following code: ``` class A { public void show() { System.out.println("A"); } } class B extends A { public void show() { System.out.println("B"); } } class C extends B { public void show() { System.out.println("C"); } } ``` What will be the output when the following code is executed? ``` A obj = new C(); obj.show(); ```
  • Consider the following class hierarchy: ``` class Animal { void makeSound() { System.out.println("Some sound"); } } class Dog extends Animal { void makeSound() { System.out.println("Bark"); } } class Cat extends Animal { void makeSound() { System.out.println("Meow"); } } ``` If we create an object like this: `Animal animal = new Dog();`, what will be printed when we call `animal.makeSound()`?
  • Consider the following code: ``` public class A { public static void doSomething() { System.out.println("Method in A"); } } public class B extends A { public static void doSomething() { System.out.println("Method in B"); } } ``` What will be the output when the following code is executed? ``` A obj = new B(); obj.doSomething(); ```


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.