Home »
Aptitude Questions and Answers »
PHP Aptitude Questions and Answers
PHP JSON Aptitude Questions and Answers
PHP JSON Aptitude Questions and Answers: This section contains aptitude questions and answers on PHP JSON.
By Nidhi Last updated : December 15, 2023
This section contains Aptitude Questions and Answers on PHP JSON.
1) There are the following statements that are given below, which of them are correct about JSON in PHP?
- JSON stands for JavaScript Object Notation.
- JSON stands for JavaScript OOPS Notation.
- JSON is a data format that is used to store and exchange data.
- JSON is a text-based format.
Options:
- A and C
- B and C
- A, C, and D
- D
Correct answer: 3
A, C, and D
Explanation:
JSON is a text-based format, which is used to store and exchange data from or to a server, JSON format is used by multiple programming languages.
2) Which of the following function is used to encode the data into JSON format?
- jsonencode()
- json_encode()
- encode_json()
- None of the above
Correct answer: 2
json_encode()
Explanation:
The json_encode() is a built-in function which is used encode data into JSON format.
3) What is the correct output of given code snippets in PHP?
<?php
$roll_no = array(
"SHAURYA" => 101,
"DUGGU" => 102,
"Joe" => 103
);
echo json_encode($roll_no);
?>
- {"SHAURYA":101,"DUGGU":102,"Joe":103}
- {"SHAURYA"->101,"DUGGU"->102,"Joe"->103}
- {"SHAURYA"=>101,"DUGGU"=>102,"Joe"=>103}
- None of the above
Correct answer: 1
{"SHAURYA":101,"DUGGU":102,"Joe":103}
Explanation:
In the above code json_encode() function encode or convert array value in the JSON format.
4) What is the correct output of given code snippets in PHP?
<?php
$students = array(
"SHAURYA",
"DUGGU",
"Joe"
);
echo json_encode($students);
?>
- {"SHAURYA","DUGGU","Joe"}
- ["SHAURYA","DUGGU","Joe"]
- <"SHAURYA","DUGGU","Joe">
- ("SHAURYA","DUGGU","Joe")
Correct answer: 2
["SHAURYA","DUGGU","Joe"]
Explanation:
The above code will print ["SHAURYA","DUGGU","Joe"] on the web page.
5) What is the correct output of given code snippets in PHP?
<?php
$students = '["SHAURYA","DUGGU","Joe"]';
echo json_decode($students);
?>
- {"SHAURYA","DUGGU","Joe"}
- Error
- Array
- None of the above
Correct answer: 3
Array
Explanation:
The json_decode() function returns an array after decoding JSON format. But we cannot print array directly, we need to use a loop to access array elements or we can access individual elements using the index.
6) What is the correct output of given code snippets in PHP?
<?php
$students = '["SHAURYA","DUGGU","Joe"]';
echo var_dump(json_decode($students));
?>
- {"SHAURYA","DUGGU","Joe"}
- ["SHAURYA","DUGGU","Joe"]
- array(3) { [0]=> string(7) "SHAURYA" [1]=> string(5) "DUGGU" [2]=> string(3) "Joe" }
- None of the above
Correct answer: 3
array(3) { [0]=> string(7) "SHAURYA" [1]=> string(5) "DUGGU" [2]=> string(3) "Joe" }
Explanation:
The json_decode() function returns array after decoding JSON formatted data, and then we print array using var_dump() then following data will be printed on web page.
7) What is the correct output of given code snippets in PHP?
<?php
$students = {"SHAURYA":101,"DUGGU":102,"Joe":103};
echo var_dump(json_decode($students));
?>
- object(stdClass)#1 (3) { ["SHAURYA"]=> int(101) ["DUGGU"]=> int(102) ["Joe"]=> int(103) }
- {"SHAURYA","DUGGU","Joe"}
- Garbage value
- Error
Correct answer: 4
Error
Explanation:
The above code will generate an error because here we did not assign value in $student variable properly, here we need to enclose data into single quotes like this:
$students = '{"SHAURYA":101,"DUGGU":102,"Joe":103}';
8) What is the correct output of given code snippets in PHP?
<?php
$students = '{"SHAURYA":101,"DUGGU":102,"Joe":103}';
$data = json_decode($students);
echo $data->DUGGU;
?>
- 102
- DUGGU
- Error
- None of the above
Correct answer: 1
102
Explanation:
The above code will print 102 on web page, here json_decode() function decodes JSON data and return an object then we can access JSON items and print them.
9) What is the correct output of given code snippets in PHP?
<?php
$students = '{"SHAURYA":101,"DUGGU":102,"Joe":103}';
$data = json_decode($students);
echo $data['DUGGU'];
?>
- 102
- DUGGU
- Error
- None of the above
Correct answer: 3
Error
Explanation:
The above code will generate an error , because in the above code json_decode() return an object, but we want to print data like associative array, then we need to pass one more argument in json_decode() function then it will return an associative array, then we need to use json_decode($students,true) function call in our code.
10) Can we transfer JSON data using the web-method call in the web service implementation?
- Yes
- No
Correct answer: 1
Yes
Explanation:
Yes, we can transfer JSON data using the web-method call in the web service implementation.